2017-12-13 3 views
0

두 테이블이 있습니다.그룹 바이 사용 및 Linq의 최소

주문 :

  • ID
  • 제품 ID
  • DeliveryDate

제품 :

  • ID
  • 이름
  • 설명

나는 모든 제품의 목록을 가장 빠른 배달 날짜로 받고 싶습니다.

다음 SQL을 사용하여 올바른 데이터를 얻고 있습니다. 배달 날짜가 ​​가장 빠른 모든 제품의 목록을 제공합니다.

SELECT 
    p.Name, p.Description, min(o.DeliveryDate) 
FROM Product p 
JOIN Order o 
On o.ProductID = p.ID 
Group By p.ID; 

저는 Linq를 사용하여 글을 쓰려고했지만 뭔가가 없습니다. 이 쿼리를 작성하는 방법을 식별 할 수 없습니다.

상대 스택 오버플로 솔루션을 사용해 보았지만 제 경우에는 도움이되지 않았습니다.

내 Linq에이다 : 그것은 제공

await (from p in dbContext.Product 
    join o in dbContext.Order 
    on o.ProductID equals p.ID 
    select new 
    { 
     p.ID, 
     p.Name, 
     p.Description, 
     o.DeliveryDate 
    }).GroupBy(g => g.ID).ToListAsync(); 

나 데이터는 이후에 가입하고 그룹. 제품에 대한 가장 빠른 DeliveryDate를 얻기 위해 어디에서 Minq을 Linq에 넣어야합니까?

+0

LINQ 코드가 맞습니까? Person 테이블과 Order 테이블을 합치고 있습니까? –

+0

젠장! 그것의 나쁜. 나는 그것을 편집했다. –

답변

1

탐색 속성이 Product.Orders 인 것으로 가정합니다. 그렇지 않은 경우 코드에 추가하는 것이 좋습니다. 그 특성을 갖는, 이것은 당신이 원하는 것을 달성 :

from p in dbContext.Product 
select new 
{ 
    p.ID, 
    p.Name, 
    p.Description, 
    MinDeliveryDate = (DateTime?)o.Orders.Min(o => o.DeliveryDate) 
} 

DateTime?에 캐스팅 제품은 주문이없는 경우 예외를 방지하는 것입니다. 어떤 이유로 당신이 탐색 속성과 이없는 경우

정말 순간에 그것을 추가 할 수 없습니다 당신은 사용할 수 있습니다 : Product에서 질의를 시작하지 합류 것을

from p in dbContext.Product 
select new 
{ 
    p.ID, 
    p.Name, 
    p.Description, 
    MinDeliveryDate = (DateTime?)dbContext.Order 
              .Where(o => o.ProductID == p.ID) 
              .Min(o => o.DeliveryDate) 
} 

Order으로 더 이상 그룹화 할 필요가 없습니다.

+0

감사합니다 나를 위해 일하는 @ GertArnold !!! :) –

1

일부 클래스를 사용하여 데이터베이스를 사용했지만이 방법이 유용합니까? 당신이 "있는 OrderBy"LINQ 방법을 사용할 수있는 그룹을 주문하려는 경우

class Product 
{ 
    public int ID; 
    public string Name; 
    public string Description; 
} 

class Order 
{ 
    public int ProductID; 
    public DateTime DeliveryDate; 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
    Product[] proTestData = new Product[] 
    { 
     new Product() {ID=1, Name="Widget", Description="Banded bulbous snarfblat"}, 
     new Product() {ID=2, Name="Gadget", Description="Hitchhiker's guide to the galaxy"} 
    }; 
    Order[] ordTestData = new Order[] 
    { 
     new Order() {ProductID=1, DeliveryDate=new DateTime(2017,12,14)}, 
     new Order() {ProductID=1, DeliveryDate=new DateTime(2017,12,20)}, 
     new Order() {ProductID=2, DeliveryDate=new DateTime(2017,12,23)}, 
     new Order() {ProductID=2, DeliveryDate=new DateTime(2017,12,22)}, 
     new Order() {ProductID=2, DeliveryDate=new DateTime(2017,12,21)}, 
     new Order() {ProductID=1, DeliveryDate=new DateTime(2017,12,18)} 
    }; 

    var rows = 
     (from p in proTestData 
     join o in ordTestData 
     on p.ID equals o.ProductID 
     group o.DeliveryDate by p into grp 
     select new { 
      grp.Key.ID, 
      grp.Key.Name, 
      grp.Key.Description, 
      minDate = grp.Min()} 
     ).ToList(); 

    foreach (var row in rows) 
    { 
     Console.WriteLine("{0}: {1}", row.Name, row.minDate); 
    } 
    } 
} 

출력은

Widget: 12/14/2017 12:00:00 AM 
Gadget: 12/21/2017 12:00:00 AM 
0

입니다.

다음을 적용하십시오.GroupBy()

.OrderBy(group => group.First().DeliveryDate).ToListAsync();