如何申请明显?

时间:2015-06-26 20:03:42

标签: linq-to-sql distinct-values

我正在尝试将Linq上的Distinct子句应用于实体查询。 查询必须具有orderby,skip和take。 Distinct功能不允许定义属性。 但这失败了

result.Data =  Context.Set<ObjectInfo>().AsExpandable().Where(filter)
   .Select(x => new ObjectInfo {             
               Id = x.Id,          
               HullId = x.HullId,    
               Name = x.Name,  
               BoatNumber = x.BoatNumber,
               ManagingOwnerFirstName = x.ManagingOwnerFirstName,
               ManagingOwnerLastName = x.ManagingOwnerLastName,                                  
               ManagingOwnerDnrId = x.ManagingOwnerDnrId })
    .Distinct().OrderBy(orderBy).Skip(skip).Take(take);

1 个答案:

答案 0 :(得分:0)

您的选择例程似乎从ObjectInfo开始并返回ObjectInfo。如果要返回所有字段,那么您实际上并不需要.select选项,即以下内容应该可以正常工作

result.Data =  Context.Set<ObjectInfo>().AsExpandable().Where(filter)
     .Distinct().OrderBy(orderBy).Skip(skip).Take(take);

如果您只想从ObjectInfo返回一些字段,那么您应该返回一个匿名对象,例如

result.Data =  Context.Set<ObjectInfo>().AsExpandable().Where(filter)
.Select(x => new   {             
           Id = x.Id,          
           HullId = x.HullId,    
           Name = x.Name,  
           BoatNumber = x.BoatNumber,
           ManagingOwnerFirstName = x.ManagingOwnerFirstName,
           ManagingOwnerLastName = x.ManagingOwnerLastName,                                  
           ManagingOwnerDnrId = x.ManagingOwnerDnrId })
.Distinct().OrderBy(orderBy).Skip(skip).Take(take);