开发者

Count and sum Intersects of an IEnumerable List of 2-tuple lists of objects in c#

开发者 https://www.devze.com 2023-02-20 03:32 出处:网络
I want to Count and sum Intersects of an IEnumerable List of 2-tuple lists of objects. For example how do i use the Intersect to get a sum of all the intersecting 2-tuples.

I want to Count and sum Intersects of an IEnumerable List of 2-tuple lists of objects. For example how do i use the Intersect to get a sum of all the intersecting 2-tuples.

var combinations = ls.Select(x => ls.Where(y => ls.IndexOf(y) > ls.IndexOf(x))
                                                          .Select(z => new List<Foo> { x, z }))
                                                   开发者_如何学Go       .SelectMany(x => x);


I'm afraid I don't completely understand what you are asking - your sample doesn't make it clear.

If it helps, there is an Intersect method in Linq: http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx


Your question is not very clear, but does this help?

 List<int> X = new List<int>();
 List<int> Y = new List<int>();

 int countOfIntersections = X.Intersect(Y).Count();
 int sumOfIntersections = X.Intersect(Y).Sum();

EDIT

Wait, are you looking for the Cartesian product of two lists?

 List<int> XList = new List<int>();
 List<int> YList = new List<int>();

 var tuples = from x in XList
              from y in YList
              select new { x, y };
0

精彩评论

暂无评论...
验证码 换一张
取 消