开发者

LINQ to Entities not returning expected result

开发者 https://www.devze.com 2023-03-07 04:11 出处:网络
I am using a view to return a complex search query. When I usin linq to query against EF it is returning the same row 3 times(the actual rowcount is correct).

I am using a view to return a complex search query. When I usin linq to query against EF it is returning the same row 3 times(the actual rowcount is correct).

using LinqPad I have run the same linq against my ef entity and the actual database view.

ReadmitPatientList
    .AsQueryable()
    .Where("PatientLastName.StartsWith(\"cooper\")")
    .OrderBy (rpl => rpl.PatientLastName)
    .Dump();

That is the linq I am using for both.

linqpad shows the lambda as this: EF:

ReadmitPatientList.MergeAs (AppendOnly)
   .Where ( => .PatientLastName.StartsWith ("cooper"))
   .Orde开发者_运维百科rBy (rpl => rpl.PatientLastName)

DB

ReadmitPatientList
   .Where ( => .PatientLastName.StartsWith ("cooper"))
   .OrderBy (rpl => rpl.PatientLastName)

I cannot post the results...but EF returns three rows of the same record. DB returns 3 rows of individual records. As does my sql query.

What about my EF LINQ do I need to change to make it work correctly?


The sql code that is generated by the EF Linq query Actually returns the correct results if run in SQL explorer.


This happens if Patient entity doesn't have primary key or columns inferred as primary key are same across multiple records in the result set. EF uses internally identity map which requires that each uniquely identified record must reuse the same instance of the entity. So if you return three records from the database which have same unique identification for EF will return enumeration of three same instances representing the first record from the result set (more about identity map also here). The same behaviour is in Linq-to-sql's DataContext.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号