2

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.

5
  • 1
    A source of error messages may be miss spelled identifiers like Slect and toList unless you have defined these methods yourself. Commented Aug 29, 2016 at 11:40
  • you miss spelling of Select Commented Aug 29, 2016 at 11:41
  • What is the type of planId? Is it a collection? Also I see the spelling of Select is wrong in the inner query. Commented Aug 29, 2016 at 11:41
  • Be sure and mark the answer that you feel helped to solve your problem. Commented Aug 29, 2016 at 11:52
  • "Code is giving error message" Next time also provide this error-message ;) Commented Aug 29, 2016 at 11:54

3 Answers 3

3

Use Enumerable.Intersect:

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

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Using Intersect is a nice approach.. But if you say what you are doing wrong I would say you are iterating on the wrong thing. The query as per your appproach should be:

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

Comments

0

Use Where to compare between both List.

List<string> result =
         reviewModel.Where(x => x.planId.Contains(reviewModel.lstPraudits.Select(y=>y.PlanId)))
         .ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.