我们可以将这个For循环转换为LINQ吗?

时间:2013-03-31 05:18:26

标签: linq

我们可以将这个For循环转换为LINQ表达式;查询语法和方法语法?

List<INode> sds = new List<INode>();

foreach (INode n in lnd)
{
    foreach(string s in Pages)
    {
        if (n.NiceUrl == s)
        {
            sds.Add(n);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

from n in lnd
from s in Pages
where n.NiceUrl == s
select n

答案 1 :(得分:1)

sds = lnd.Join(Pages, n => n.NiceUrl, p => p, (n, p) => n).ToList();

答案 2 :(得分:0)

Pages.Where(y => lnd.Select(x => x.NiceUrl).Contains(y)).Tolist();

lnd.Select(x =&gt; x.NiceUrl)部分可以用HashSet替换。

相关问题