问题描述:

I have two table A and B.

Table A is like:

Date | Value

1/9/2013 13:30 | 1

1/9/2013 13:31 | 2

1/9/2013 13:32 | 2.1

1/9/2013 13:33 | 1.6

1/9/2013 13:34 | 1.7

1/9/2013 13:35 | 1.9

1/9/2013 13:36 | 5.6

1/9/2013 13:37 | 1.6

1/9/2013 13:38 | 1.64

1/9/2013 13:39 | 1.36

And table B is like:

1/9/2013 13:33 | 1.7

1/9/2013 13:37 | 1.6

1/9/2013 13:38 | 1.8

I want to have a table C which will have all entries of B, but if there is not entry at 13:30 for example the value is null. So Table C is like:

 Date | Value

1/9/2013 13:30 | null

1/9/2013 13:31 | null

1/9/2013 13:32 | null

1/9/2013 13:33 | 1.7

1/9/2013 13:34 | null

1/9/2013 13:35 | null

1/9/2013 13:36 | null

1/9/2013 13:37 | 1.6

1/9/2013 13:38 | 1.8

1/9/2013 13:39 | null

I know that one can do that with a join and linq , do you have any idea ?

Thanks

网友答案:

Try following LEFT JOIN query:

from a in dtA.AsEnumerable()
join b in dtB.AsEnumerable() on a.Field<DateTime>("Date") equals b.Field<DateTime>("Date") into joinedB
from b in joinedB.DefaultIfEmpty()
select new
{
    Date = a.Field<DateTime>("Date"),
    Value = b != null ? b.Field<DateTime>("Value") : null
}
相关阅读:
Top