获取具有不同ShipperName的实体结果

时间:2014-01-20 18:38:21

标签: c# entity-framework-5

我有一个返回多个结果集的查询。

List<FromAddress> address1 = (from a in stEntities.ShipFromInfoes
                                join b in stEntities.Addresses on a.AddressId equals b.AddressId
                                              where a.ShipperName.StartsWith(namestartwith)                                
                                select new FromAddress()
                                {
                                    ShipperName = a.ShipperName,
                                    TAddressId = a.AddressId,
                                    AddressLine1 = b.AddressLine1,
                                    AddressLine2 = b.AddressLine2,
                                    AddressLine3 = b.AddressLine3,
                                    TelephoneNumber = b.PhoneNumber,
                                    FaxNumber = b.FaxNumber,
                                    MobileNumber = b.MobileNumber
                                }).Distinct().ToList();

在address1结果中,我有多个具有相同ShipperName的结果。如何使用具有唯一ShipperName的所有文件获取结果。

1 个答案:

答案 0 :(得分:0)

嗯,你没有说为每个独特的发货人采取哪个地址,但是要使用第一个地址:

select new FromAddress()
{
...
})
.GroupBy(fa => fa.ShipperName)
.Select(g => g.First())