Reputation: 758
I'm trying to retrieve a unique element if it exist, or null if it doesn't.
my function is :
private Dealer GetOrCreateDealer(DataRow dataRow)
{
ModelCamwareContext localContext = new ModelCamwareContext();
string test = dataRow["Dealer"].ToString();
Dealer dealer = localContext.Dealers.Where(x => x.Name == dataRow["Dealer"].ToString()).FirstOrDefault();
if (dealer != null)
{
...
}
else
{
...
}
}
my test string is just here to make sure I had no problem with the dataRow info, it works as expected and gives me the string on which I want to select my dealer.
Unless I am mistaken, FirstOrDefault should not give me any exception in case there is no dealer to retrieve, I should in that case have dealer = null.
The exception I get:
A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll
What did I miss ?
Upvotes: 0
Views: 971
Reputation: 513
Entity cannot evaluate datatables values inside it's LINQ. Try something like this:
ModelCamwareContext localContext = new ModelCamwareContext();
string test = dataRow["Dealer"].ToString();
Dealer dealer = localContext.Dealers.Where(x => x.Name == test).FirstOrDefault();
if (dealer != null)
{
}
else
{
}
Upvotes: 1