1

I have list of string of unique keys

  var uniqueKeys = new List<string>   {"01", "04", "09", "26", "27"};

I am trying to filter another list based on these unique keys.

Data in the list can be seen like this :

To  From    
01  05
01  02
09  04
01  09
01  45
04  06
27  12

I want to select data from this list in a way that both "To" and "From" property values lies in uniqueKeys

Desired Result should be :

To  From
09  04
01  09

I have been through many posts over internet an I am not able to write the logic in simple LINQ format.

If someone has faced this problem please help me.

siddharth
  • 149
  • 5
  • 13

5 Answers5

8

Lets say your list of class name is "listToFrom". You can use the && operator on both the properties to get the desired list.

var filteredList = listToFrom.Where(x=>uniqueKeys.Contains(x.To) && 
                    uniqueKeys.Contains(x.From)).ToList();
Harsh
  • 3,683
  • 2
  • 25
  • 41
3

You can put several statements in a .Where so you can filter on multiple fields, the key in this case is .Contains.

//Making test data
List<Example> dataList = new List<Example>()
{
    new Example() { From = "05", To = "01" },
    new Example() { From = "02", To = "01" },
    new Example() { From = "04", To = "09" },
    new Example() { From = "09", To = "01" },
    new Example() { From = "45", To = "01" },
    new Example() { From = "06", To = "04" },
    new Example() { From = "12", To = "27" }
};
var uniqueKeys = new List<string> { "01", "04", "09", "26", "27" };

//Filter data
var filteredList = dataList
                    .Where( row => uniqueKeys.Contains( row.To ) && 
                                    uniqueKeys.Contains( row.From ) )
                    .ToList();
EpicKip
  • 4,015
  • 1
  • 20
  • 37
2

You can use .Contains to check if a collection contains an item. Also, you can utilize LINQ .Where to filter a collection with specified condition.

var uniqueKeys = new List<string> {"01", "04", "09", "26", "27"};
var result = data
    .Where(x => uniqueKeys.Contains(x.From) && uniqueKeys.Contains(x.To)) 
    .ToArray();

Another one thing which should be kept in mind - this algorithm is linear, i.e. it will iterate uniqueKeys every time to check if it contains a value. Performance can be improved by using HashSet which provides O(1) .Contains check.

var uniqueKeys = new List<string> {"01", "04", "09", "26", "27"};
var uniqueKeysSet = new HashSet<string>(uniqueKeys);
var result = data
    .Where(x => uniqueKeysSet.Contains(x.From) && uniqueKeysSet.Contains(x.To)) 
    .ToArray();

However, you can skip this performance improvement if the number of items in uniqueKeys is small. It will overcomplicate code without necessity.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

This can be done by combining two .Contains() LINQ clauses as follows:

var uniqueKeys = new List<string>   {"01", "04", "09", "26", "27"};

var data = new List<Tuple<string, string>> {
new Tuple<string, string>("01", "05"),
new Tuple<string, string>("01", "02"),
new Tuple<string, string>("09", "04"),
new Tuple<string, string>("01", "09"),
new Tuple<string, string>("01", "45"),
new Tuple<string, string>("04", "06"),
new Tuple<string, string>("27", "12")
};

var results = data.Where(d => uniqueKeys.Contains(d.Item1) && uniqueKeys.Contains(d.Item2));
Chris Pickford
  • 8,642
  • 5
  • 42
  • 73
0

Try

    var uniqueKeys = new List<string>   {"01", "04", "09", "26", "27"};


    List<Example> filtedvalues = Maindatas.FindAll(emp => uniqueKeys == emp.to && uniqueKeys == emp.from);