C
C#12mo ago
popcorn

Full outer join two different structures - LINQ

Hello, is it possible to do following?
class Item
{
public DateTime Date {get; set;}
public decimal Price {get; set;}
public string Name {get; set;}
}

class Request
{
public DateTime Date {get; set;}
public decimal PayedAmount {get; set;}
}

class MergedItem
{
public DateTime Date {get; set;}
public decimal Price {get; set;}
public string Name {get; set;}
public decimal PayedAmount {get; set;}
}

List<Item> items;
List<Request> requests;
// How can I do something like
var final = items.UnionOnKey(requests, "Date").Select(x => new MergedItem()
{
Date = x.item != null ? x.item.Date : x.request.Date,
Price = x.item ? x.item.Price : 0m,
Name = x.item ? x.item.Name : "",
PayedAmount x.request ? x.request.PayedAmount : 0m
});
class Item
{
public DateTime Date {get; set;}
public decimal Price {get; set;}
public string Name {get; set;}
}

class Request
{
public DateTime Date {get; set;}
public decimal PayedAmount {get; set;}
}

class MergedItem
{
public DateTime Date {get; set;}
public decimal Price {get; set;}
public string Name {get; set;}
public decimal PayedAmount {get; set;}
}

List<Item> items;
List<Request> requests;
// How can I do something like
var final = items.UnionOnKey(requests, "Date").Select(x => new MergedItem()
{
Date = x.item != null ? x.item.Date : x.request.Date,
Price = x.item ? x.item.Price : 0m,
Name = x.item ? x.item.Name : "",
PayedAmount x.request ? x.request.PayedAmount : 0m
});
I simply need to include both a and b if the other one is null as well as match the two together
1 Reply
cap5lut
cap5lut12mo ago
u would do that with an left outer join, as far as i can see thats done with a group join combined with an select many https://dotnettutorials.net/lesson/left-outer-join-in-linq/
Dot Net Tutorials
Dot Net Tutorials
LINQ Left Join in C#
In this article, I am going to discuss LINQ Left Outer Join in C# with Examples. The Left Join in C# is also called Left Outer Join in LINQ.