LINQ联同过滤标准 [英] LINQ join with filter criteria

查看:121
本文介绍了LINQ联同过滤标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是从这个问题:<一href="http://stackoverflow.com/questions/1401889/sql-filter-criteria-in-join-criteria-or-where-clause-which-is-more-efficient">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) };

注意 - 更正的组线错字

这篇关于LINQ联同过滤标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆