Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading