考虑到LINQ to Excel的问题

我正在读取来自XLS文档的数据,并使用了极好的LINQ to Excel库。 我遇到的问题更多的是与处理LINQ的问题。

我从Excel工作表中读取新的和更新的事件。 所以我想检查事件是否已经存在于数据库中,如果是这样,我想把它与事件挂钩,然后用我读过的所有新数据更新它。 一些代码:

var excel = new ExcelQueryFactory("filepath"); var getincident = from jj in excel.Worksheet<Incident>("Sheet1") select jj; foreach (var incident in getincident) { if (incident.CallId.Trim() == "") break; if (exists(incident.CallId, context)) { incident.id = (from b in context.Incidents where b.CallId == incident.CallId select b.id ).First(); context.Incidents.Attach(incident, true); } else { context.Incidents.InsertOnSubmit(incident); } context.SubmitChanges(); } 

如果事件存在,存在就是一个简单的检查:

 private bool exists(string piCallId, DataClasses1DataContext context) { return (from b in context.Incidents where b.CallId == piCallId select b ).Any(); } 

首先需要一些方法来检查事件是否存在,然后提交所有的新数据。 请帮忙。

这是做你想要的吗?

 var existingIncident = (from b in context.Incidents where b.CallId == incident.CallId select b ).SingleOrDefault(); if (existingIncident != null) { existingIncident.xxx = incident.xxx; existingIncident.yyy = incident.yyy; ... } else context.Incidents.InsertOnSubmit(incident);