LINQ联同过滤标准标准、LINQ

2023-09-06 07:12:22 作者:看!灰鸡

怎么是这样的呢?它的JOIN筛选条件。

How is something like this done in linq? It has filter criteria on the JOIN.

这是从这个问题:http://stackoverflow.com/questions/1401889/sql-filter-criteria-in-join-criteria-or-where-clause-which-is-more-efficient

select salesman.salesmanid, max(sales.quantity)
from salesman
inner join sales  on salesman.salesmanid =sales.salesmanid 
              and sales.salesdate < salesman.promotiondate
group by salesman.salesmanid

感谢

推荐答案

您不能加入以外的任何其它等于,但是这可能不是你想要在这里反正什么。我会争辩说,SQL查询笨拙的书面和日期比较应该在,其中条款,但我想这是主观的。无论如何,这是该的只有的方式做到这一点的LINQ的:

You can't join on anything other than equals, but that's probably not what you want here anyway. I would contend that the SQL query is awkwardly written and that the date comparison should be in a WHERE clause, but I suppose that's subjective. Anyway, that's the only way to do it in Linq:

var results =
    from sm in salesman
    join s in sales on sm.salesmanid equals s.salesmanid
    where s.salesdate < sm.promotiondate
    group s by s.salesmanid into g
    select new { salesmanid = g.Key, maxsales = g.Max(s => s.quantity) };

注意 - 更正的组线错字的