2010-06-26 56 views
0

我有人士作了訂單表:排名。 LINQ to SQL的問題

Orders 
{ 
    Guid PersonId, 
    int Priority, 
    Guid GoodId 
} 

當務之急是某一整數。例如:

AlenId 1 CarId 
DianaId 0 HouseId 
AlenId 3 FoodId 
DianaId 2 FlowerId 

我想要檢索最高優先級順序爲每個人:

AlenId 1 CarId 
DianaId 0 HouseId 

在T-SQL我會用排序,我怎麼能得到相同的結果Linq中-2- Sql?

預先感謝您!

回答

2

事情是這樣的:

var query = from order in context.Orders 
      orderby order.Priority 
      group order by order.PersonId into personOrders 
      select personOrders.First(); 

相信應該工作,但我不知道如何定義它,在訂貨後的分組方面。這也將工作,雖然這是稍微難看:

var query = from order in context.Orders 
      group order by order.PersonId into personOrders 
      select personOrders.OrderBy(order => order.Priority).First(); 

或者只用點符號:

var query = context.Orders 
        .GroupBy(order => order.PersonId) 
        .Select(group => group.OrderBy(order => order.Priority) 
             .First());