2016-11-06 105 views
0

我有兩個表,tblItem和tblInsertLines,在tblInsertLines中我有相同的ItemId但有不同的ProdDate和ExpireDate,我想獲取所有項目的不同列表,但選擇第一行tblInsertLines作爲第一行包含最早的ProdDate。 任何幫助將不勝感激。我使用這個代碼。從多個表中選擇不同從linq到sql

public static List<Item> getItemList() 
    { 
     using (var db = new AWarehouseDataClassesDataContext()) 
     { 

      var list = (from i in db.tblItems 
         join e in db.tblInsertLines on i.ItemId equals e.ItemId 
         orderby i.NameE 
         select new Item 
         { 
          code = i.Code, 
          itemId = i.ItemId, 
          lastUpdate = i.LastUpdate, 
          nameA = i.NameA, 
          nameE = i.NameE, 
          qty = i.Qty, 
          prodDate = e.ProdDate, 
          expireDate = e.ExpireDate, 
          updatedBy = i.UpdatedBy 
         }).Distinct(); 
      return list.ToList(); 
     } 
    } 
+0

你要根據最古老的ProdDate拿到第一排? – Eldeniz

+0

是的。或根據最早插入的行(lineId)。這也會給最老的插入的一個。 –

回答

1

您可以嘗試

var list= (from i in db.tblItems 
       join e in db.tblInsertLines on i.ItemId equals e.ItemId 
       where e.counter > 0 
       orderby i.NameE 
       group new { i, e } by e.ItemId into g 
       select new Item 
       { 
        code = g.First().i.Code, 
        itemId = g.Key, 
        lastUpdate = g.First().i.LastUpdate, 
        nameA = g.First().i.NameA, 
        nameE = g.First().i.NameE, 
        qty = g.First().i.Qty, 
        prodDate = g.Min(x=>x.e.ProdDate), 
        expireDate = g.First().e.ExpireDate, 
        updatedBy = g.First().i.UpdatedBy 
       }).ToList(); 
+0

請注意我必須返回列表。不能使用list.ToList(); –

+0

第()方法返回唯一項目,對象沒有列出 Eldeniz

+0

可以返回項目,而不是列出 Eldeniz