开发者

How to return distinct rows from and Entity with related Entities

开发者 https://www.devze.com 2023-03-19 09:57 出处:网络
I have an Entity that has an association to other Entities (related entities). I\'m trying to return distinct rows from the primary entity which needs to include the data from the related entity so I

I have an Entity that has an association to other Entities (related entities). I'm trying to return distinct rows from the primary entity which needs to include the data from the related entity so I can use one the related entity's properties downstream.

Below is the statement I'm using but it is not returning any rows. What's the best way to do this?

Below is my code.

return context.UserDisplays.Include("CurrentJob").Where(d => d.UserName == userNa开发者_如何转开发me).GroupBy(d => d.CurrentJob.JobNo).Select(g => g.FirstOrDefault()).ToList();

Any help would be greatly appreciated!


Edit - For ComplexProperty

I believe once you do a GroupBy all Include methods are ignored. So you will need to iterate the list and call the LoadProperty method on each item. It should look something like this

var list = context.UserDisplays
                  .Where(d => d.UserName == userName)
                  .GroupBy(d => d.CurrentJob.JobNo)
                  .Select(g => g.FirstOrDefault()).ToList();

foreach(var item in list)
{
   context.LoadProperty(item, "CurrentJob");
}

return list;

Resource Link


Check out the Distinct (Set Operators) section in this article http://msdn.microsoft.com/en-us/vcsharp/aa336746 Are you asking for the Distinct UserDisplays? or the Distinct User or the Disticnt Jobs?

I would try say something like

var object = (from userDisplay in context.UserDisplays.Include("CurrentJob")
.Where userDisplay.UserName == userName
Select userDisplay).Distinct();

(sorry, im going off of my VB style but it should be about the same...)

0

精彩评论

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

关注公众号