c# - Finding or creating an object in Entity Framework -
my domain model , associations follows:
customer
has manyregion
sregion
has manylocation
s
our client supplies csv file has following columns:
- customer name
- region name
- location name
- latitude
- longitude
- ...
based on information, have find or create customer name, find or create region name, , find or update location name.
i have tried following:
var customer = c in _data.customer c.name == cells[0] select c; if (customer == null) customer = new customer(...);
i follow same pattern finding or creating/updating region , location, however, problem run type of linq query cannot converted customer
-object on line customer = new customers();
. need reference customer
object later can't have 2 separate variables.
how accomplish in entity framework?
this code
var customer = c in _data.customer c.name == cells[0] select c;
returns customers has name
equals cells[0]
return type iqueryable<customer>
not sigle customer
(that's why customer = new customer(...)
doesn't work).
what need call firstordefault
or singleordefault
(depending on requirements.) on if want have 1 customer
.
so should work
var customer = (from c in _data.customer c.name == cells[0] select c).singleordefault();
Comments
Post a Comment