Search code examples
c#lambdacollectionsintersection

c# intersection two collections returns collection with intersected index and new field


I have two collections:

IEnumerable<int> data1 = new List<int> {101, 102, 103, 104};
IDictionary<int, int> data2 = new Dictionary<int, int> 
{
   { 101, 1 }, 
   { 104, 0 }
};

I want do to something like

var newData = data2.intersect(data2, lambda expression);

newData should be a collection of IEnumerable<int, bool>

being the first field the intersection of data1 == data2.Key and the second field the evulation of value == 1


Solution

  • I think you want a join between both, which is like an intersection:

    var query = from d1 in data1
                join d2 in data2 on d1 equals d2.Key
                select (Value: d1, Result: d2.Value == 1);
            
    IEnumerable<(int Value, bool Result)> resultList = query.ToList();
    

    The result list contains two items:

    101 True
    104 False