c# Find the intersection of two lists<> and create a new list with merged properties

时间:2017-04-10 02:01:09

标签: c# linq

I have two Lists - A & B include {time, value}.

List<TimeVal> A = ...;
List<TimeVal> B = ...;

public class TimeVal
{
    public DateTime time {get;set;}
    public int value {get;set;}
}

How can I find the intersection where A.time == B.time to generate a new List which merges the properties of both C= {time, valueA, valueB} ?

List<TimeBothVals> C = ?
public class TimeBothVals
{
public DateTime time {get;set;}
public int ValueA {get;set;}
public int ValueB {get;set;}
}

Thank you.

1 个答案:

答案 0 :(得分:0)

你走了。

List<TimeBothVals> C = (from ai in a
                        join bi in b on ai.time equals bi.time
                        select new TimeBothVals
                                {
                                    time = ai.time,
                                    ValueA = ai.value,
                                    ValueB = bi.value
                                })
                        .ToList();