Subodh Shetty
Subodh Shetty

Reputation: 11

Can we convert this For-loop into LINQ?

Can we convert this For-loop into LINQ expresion; both in Query Syntax and Method Syntax ?

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

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

Upvotes: 0

Views: 74

Answers (3)

Amit Khanna
Amit Khanna

Reputation: 489

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

Upvotes: 1

Peter Kiss
Peter Kiss

Reputation: 9319

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

lnd.Select(x => x.NiceUrl) part can be replaced with a HashSet.

Upvotes: 0

Bryan Watts
Bryan Watts

Reputation: 45445

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

Upvotes: 1

Related Questions