У меня есть два объекта под названием CountryMobility, которые, я считаю, мне нужно объединить с полным внешним соединением. Как это сделать с помощью linq?Linq Full Outer Join on Two Objects
public class CountryMobility
{
public string countryCode { get; set; }
public int inbound { get; set; }
public int outbound { get; set; }
}
Я хочу, чтобы объединить два из этих объектов, как так:
inboundStudents:
countryCode | inbound | outbound
EG | 2 | 0
CA | 3 | 0
CH | 5 | 0
outboundStudents:
countryCode | inbound | outbound
PE | 0 | 1
CA | 0 | 4
CH | 0 | 5
-
-
-
-
V
combinedStudents:
countryCode | inbound | outbound
PE | 0 | 1
CA | 3 | 4
CH | 5 | 5
EG | 2 | 0
Я попытался следующие заявления Linq, но не смогли выяснить правильный синтаксис. В настоящее время я получаю синтаксическую ошибку около temp.DefaultIfEmpty (new {first.ID, inbound = 0, outbound = 0}) в обоих операторах.
var leftOuterJoin =
from first in inboundActivities
join last in outboundActivities
on first.countryCode equals last.countryCode
into temp
from last in temp.DefaultIfEmpty
(new { first.countryCode, inbound = 0, outbound=0 })
select new CountryMobility
{
countryCode = first.countryCode,
inbound = first.inbound,
outbound = last.outbound,
};
var rightOuterJoin =
from last in outboundActivities
join first in inboundActivities
on last.countryCode equals first.countryCode
into temp
from first in temp.DefaultIfEmpty
(new { last.countryCode, inbound = 0, outbound = 0 })
select new CountryMobility
{
countryCode = last.countryCode,
inbound = first.inbound,
outbound = last.outbound,
};
var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);
возможного дубликата [LINQ - полное внешнее соединение] (http://stackoverflow.com/questions/5489987/linq-full-external-join) –
Какие заявления linq вы пробовали, что они производили? Что с ними было неправильно, что вам нужна помощь? – Alex
@ Josh L. Пробовал это, но я не мог заставить его работать правильно с моим. Я думаю, проблема заключалась в том, что он выводил анонимный объект, и я не мог заставить его выводить его как объект «CountryMobility». –