I'm reading data from an XLS document, and I'm using the superb LINQ to Excel library. The problem I have is more of a problem with dealing with LINQ.
I read new and updated incidents from an excel sheet. So I want check if the incident already exists in the database, and if it does I want to hook it up with that incident and then update it with all the new data from the excel that I've read. Some code:
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();
}
and the exists is a simple check if the incident exists:
private bool exists(string piCallId, DataClasses1DataContext context)
{
return (from b in context.Incidents
where b.CallId == piCallId select b
).Any();
}
I need some way first to check if the incident exists before all the new data is added, and then submit the changes. Please help.