PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 3, 2022

[FIXED] how to search a List<list<int>> with another List<int> in c#

 November 03, 2022     c#, lambda, linq     No comments   

Issue

I have the following lists of list.

List<List<int>> paths = new List<List<int>>();
paths.Add(new List<int>() { 0,1 });
paths.Add(new List<int>() { 0, 2 });
paths.Add(new List<int>() { 0, 4 });
paths.Add(new List<int>() { 1, 2 });
paths.Add(new List<int>() { 0, 3 });

and I have another List of int like this

List<int> ends = new List<int>(){3,4};

Now I need to filter records where any number in the inner list in paths contains any numbers from the ends list, which for this example would be the following records.

{0,3}
{0,4} 

I tried something like this

paths.Where(x => x.Where(y => ends.Contains(y))).ToList();

Solution

 paths.Where(x => x.Any(z => ends.Contains(z)))


Answered By - Hogan
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing