Add values ​​from two datatypes to new datatable

I have two strongly typed Datatable ( dt1

):

|FirstName|LastName|Val1|Val2|
|Tony     |Stark   |34  |35  |
|Steve    |Rogers  |12  |23  |
|Natasha  |Romanoff|2   |100 |

      

and the second ( dt2

)

|FirstName|LastName|Val1|Val2|
|Tony     |Stark   |16  |5   |
|Bruce    |Banner  |2   |1   |
|Steve    |Rogers  |54  |40  |

      

I am trying to create a new Datatable where I add values ​​for people. I need an outer join as I need all the people and the value in the second table is cut in half.

So, the result should look like this:

|FirstName|LastName|Val1|Val2|
|Tony     |Stark   |42  |37.5|
|Steve    |Rogers  |39  |43  |
|Natasha  |Romanoff|2   |100 |
|Bruce    |Banner  |1   |0.5 |

      

My approach was with LINQ:

Dim query = 
from a in ds1.Table1
Join b in ds2.Table2
On a.FirstName + a.LastName Equals b.FirstName + b.Lastname
Select New With {
.FirstName = a.FirstName,
.LastName = a.LastName,
.Val1 = a.Val1 + b.Val1 *0.5,
.Val2 = a.Val2 + b.Val2 *0.5
}

      

But I don't understand all people with an approach. I have also tried

Dim query = 
From a in ds1.Table1
From b in ds2.Table2
Select New With{
Key .KeyName = a.FirstName + a.LastName = b.FirstName + b.FirstName,
.Val1 = a.Val1 + b.Val1 *0.5,
.Val2 = a.Val2 + b.Val2 * 0.5
}

      

Now I get a lot of entries for each person. Can anyone help me to do this. I don't know if there is another non-Linq approach to solve this problem.

+3


source to share


2 answers


An example using group by

. The fact is that before performing operations group by

, Sum

all values ​​in the second table must be divided by 2. I will take care of this before.Concat

var dt1 = new List<MyClass>();
dt1.Add(new MyClass { FirstName = "Tony", LastName = "Stark", Val1 = 34, Val2 = 35});
dt1.Add(new MyClass { FirstName = "Steve", LastName = "Rogers", Val1 = 12, Val2 = 23});
dt1.Add(new MyClass { FirstName = "Natasha", LastName = "Romanoff", Val1 = 2, Val2 = 100 });


var dt2 = new List<MyClass>();
dt2.Add(new MyClass { FirstName = "Tony", LastName = "Stark", Val1 = 16, Val2 = 5 });
dt2.Add(new MyClass { FirstName = "Bruce", LastName = "Banner", Val1 = 2, Val2 = 1 });
dt2.Add(new MyClass { FirstName = "Steve", LastName = "Rogers", Val1 = 54, Val2 = 40 });


var q = from a in dt1
                .Concat(
                    from b in dt2 
                    select new MyClass 
                        {
                            FirstName = b.FirstName,
                            LastName = b.LastName,
                            Val1 = b.Val1 * 0.5m,
                            Val2 = b.Val2 * 0.5m
                        })
        group a by new {a.FirstName, a.LastName}
        into g
        select new
            {
                g.First().FirstName,
                g.First().LastName,
                Val1 = g.Sum(x => x.Val1),
                Val2 = g.Sum(x => x.Val2),
            };


foreach (var s in q)
{
    Console.WriteLine("{0} {1} {2} {3}", s.FirstName,s.LastName,s.Val1,s.Val2);
}

      



Result

Tony Stark 42,0 37,5
Steve Rogers 39,0 43,0
Natasha Romanoff 2 100
Bruce Banner 1,0 0,5

      

0


source


I got it now. My decision:

Dim qLeft = From a in dt1.Getdt1Info
                Group Join b in dt2.Getdt2Info
                On a.FirstName + a.LastName Equals b.FirstName + b.LastName  
                Into NewGroup = Group
                From c in NewGroup.DefaultIfEmpty
                Select New With{
                .Name = a.FirstName + ", " + a.LastName,
                .Value1 = a.Val1 + If (c is nothing, 0D, c.Val1) * 0.5,
                .Value2 = a.Val2 + If (c is nothing, 0D, c.Val2) * 0.5      
                }

Dim qRight =From a in dt2.Getdt2Info
                Group Join b in dt1.Getdt1Info  
                On a.FirstName + a.LastName Equals b.FirstName + b.LastName
                Into NewGroup = Group
                From c in NewGroup.DefaultIfEmpty
                Select New With{
                .Name = a.FirstName + ", " + a.LastName,
                .Value1 = a.val1 * 0.5 + If (c Is Nothing, 0D, c.Val1),
                .Value2 = a.val2 * 0.5 + If (c Is Nothing, 0d, c.Val2)}

Dim qFull = qleft.Concat(qRight).GroupBy(function(x) x.Name).Select(function(x) x.First)

      



I still don't know why qleft.Union (qRight) doesn't remove duplicates. I solved it with functions (s. Code).

0


source







All Articles