0

I have a query I know how to do in SQL but struggling to figure out the LINQ query. Here is the SQL.

SELECT ordNo, tranNo, COUNT(distinct custNo)
FROM orders
GROUP BY ordNo, tranNo
HAVING COUNT(distinct custNo) > 1

I don't feel like this is the same question that I see you marked as a duplicate. The linked question only groups on a single property. I've lost track of the Linq queries I've tried but here is one.

var countList = from o in orders
            group o by new {o.orderNo, o.tranNo, o.custNo}
            into grp
            where grp.Key.custNo.Distinct().Count() > 1
            select grp;

I tried the suggestion below but like someone commented you can't access the custNo property.

Lee Watson
  • 21
  • 1
  • 4

1 Answers1

0

Just spitballing since I don't know the table structure.

context.orders
    .GroupBy(o => new { o.ordNo, o.tranNo, o.custNo })
    .Where(o => o.custNo.Distinct().Count() > 1)
    .Select(o => new {
        ordNo = o.ordNo,
        tranNo = o.tranNo
    });
Dan S.
  • 173
  • 1
  • 14