Thursday, 15 May 2014

c# - Entity Framework Linq Query Plus Sql Function Nested Query -



c# - Entity Framework Linq Query Plus Sql Function Nested Query -

i have next linq query

public ienumerable<dealershipwithdealersviewmodel> get(float latitude, float longitude) { homecoming dealer in db.dealerships bring together in db.nearestdealers(latitude, longitude) on dealer.dealerid equals i.dealerid select new dealershipwithdealersviewmodel { dealerid = dealer.dealerid, dealer = dealer.dealer, dosales = dealer.dosales, doservice = dealer.doservice, addressprovinceid = dealer.addressprovinceid, addresslocationid = dealer.addresslocationid, address1 = dealer.address1, address2 = dealer.address2, tel = dealer.tel, fax = dealer.fax, maplat = dealer.maplat, maplong = dealer.maplong, location = dealer.location.locationname, dealerusers = dealer.dealerusers .select(y => new dealeruserviewmodel { dealeruserid = y.dealeruserid, firstname = y.firstname, surname = y.surname, landline = y.landline, email = y.email, position = y.dealertype.dealerposition }) }; }

maintain getting next error the nested query not have appropriate keys. cannot find on net. if load above without dealerusers, works expected, need nested data. give thanks you! below works way.

public ienumerable<dealershipwithdealersviewmodel> get(float latitude, float longitude) { homecoming dealer in db.dealerships bring together in db.nearestdealers(latitude, longitude) on dealer.dealerid equals i.dealerid select new dealershipwithdealersviewmodel { dealerid = dealer.dealerid, dealer = dealer.dealer, dosales = dealer.dosales, doservice = dealer.doservice, addressprovinceid = dealer.addressprovinceid, addresslocationid = dealer.addresslocationid, address1 = dealer.address1, address2 = dealer.address2, tel = dealer.tel, fax = dealer.fax, maplat = dealer.maplat, maplong = dealer.maplong, location = dealer.location.locationname }; }

update

this works.

return db.dealerships.select(x => new dealershipwithdealersviewmodel { dealerid = x.dealerid, dealer = x.dealer, dosales = x.dosales, doservice = x.doservice, addressprovinceid = x.addressprovinceid, addresslocationid = x.addresslocationid, address1 = x.address1, address2 = x.address2, tel = x.tel, fax = x.fax, maplat = x.maplat, maplong = x.maplong, location = x.location.location1, dealerusers = x.dealerusers.select(y => new dealeruserviewmodel { dealeruserid = y.dealeruserid, firstname = y.firstname, surname = y.surname, landline = y.landline, email = y.email, position = y.dealertype.dealertype1 }) });

it's matter of composablity. ef seek translate query sql. if succeeds, fine. if doesn't, it's not going seek , create work e.g. switching linq objects below hood (as linq sql may do). trying bring together stored procedure result sql query. can't done in plains sql, because sproc results not composable, allow lone ef.

you van bring together results in memory, using db.dealerships.asenumerable() , db.nearestdealers(latitude, longitude).

so useful if add together filter parameter dealerid procedure's signature.

c# linq entity-framework

No comments:

Post a Comment