2015-06-26 53 views
0

我想在一個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); 
+2

'但是這個失敗'這是什麼意思?你得到什麼類型的例外? –

+0

錯誤消息:無法在LINQ to Entities查詢中構造實體或複雜類型'ObjectInfo' – guru

回答

0

你選擇常規似乎開始ObjectInfo並與ObjectInfo返回。如果要返回所有字段,那麼你並不真正需要的。選擇選項,即下面應該工作

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);