Ritesh Gupta Ritesh Gupta - 1 year ago 94
C# Question

Comparing two list using lamda expression

I want to get a list of string based in two lists lstJobs and lstPraudits. I want to get PlanId which is common in both list.

Here is my code-

List<string> result=reviewModel.lstJobs.Select(x=>x.planId.Contains(reviewModel.lstPraudits.Slect(y=>y.PlanId).toList()));

What I am doing wrong here. Code is giving error message.

Answer Source

Use Enumerable.Intersect:

List<string> result = reviewModel.lstJobs.Select(x=> x.PlanId)
   .Intersect(reviewModel.lstPraudits.Select(y=> y.PlanId))

What I am doing wrong here

Your approach is wrong because x.planId.Contains will search substrings and you are passing a list to the method. It's the wrong approach anyway because you don't want to compare substrings.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download