LINQ JOIN
-
Hi, suppose I have
var A = new[]
{
new {Name ="Car", N = 2},
new {Name ="Bike", N = 100}
};var B = new\[\] { new {Name ="Car", N = 10}, new {Name ="Plane", N = 1} };
I would like to to the following using LINQ syntax: Join A nd B getting a total collection summing also N where same objects are the same... to be clear the final output should be:
{Name ="Car", N = 12},
{Name ="Bike", N = 100},
{Name ="Plane", N = 1}Thanks for your time
-
Hi, suppose I have
var A = new[]
{
new {Name ="Car", N = 2},
new {Name ="Bike", N = 100}
};var B = new\[\] { new {Name ="Car", N = 10}, new {Name ="Plane", N = 1} };
I would like to to the following using LINQ syntax: Join A nd B getting a total collection summing also N where same objects are the same... to be clear the final output should be:
{Name ="Car", N = 12},
{Name ="Bike", N = 100},
{Name ="Plane", N = 1}Thanks for your time
-
Here you go:
var query = from item in A.Union(B)
group item by item.Name into grp
select new { Name = grp.Key, N = grp.Sum(t => t.N) };modified on Wednesday, July 20, 2011 10:46 AM
Suppose now I have many var. So not only two (A and B) but many, let's say A,B,C.... Is it still managable? (I mean a union between many and not anly two)
-
Suppose now I have many var. So not only two (A and B) but many, let's say A,B,C.... Is it still managable? (I mean a union between many and not anly two)
Why not try it out and see what effect it has?
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Why not try it out and see what effect it has?
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
I'm only able to do union between two at time. My question is if I can do between many at the same time.
-
I'm only able to do union between two at time. My question is if I can do between many at the same time.
And still I ask, why not try it out for yourself? You have the basic knowledge now, so it cannot be that hard for you to formulate your own opinion. Still, you can one vote this answer as well because I haven't spoonfed you the answer, rather I've suggested that you should now be able to think for yourself.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility