siddharth
siddharth

Reputation: 159

LINQ filter Elements based on two properties

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.

Upvotes: 1

Views: 5120

Answers (5)

Harsh
Harsh

Reputation: 3751

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();

Upvotes: 8

user6448640
user6448640

Reputation:

Try

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


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

Upvotes: 0

EpicKip
EpicKip

Reputation: 4043

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();

Upvotes: 3

Chris Pickford
Chris Pickford

Reputation: 8991

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));

Upvotes: 0

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

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.

Upvotes: 2

Related Questions