How can I join different property object collections

Viewed 93

I have a list of my issue class

var firstList = new List<object> {
    new { id =1, Name = "item-1"},
    new { id =2, Name = "item-2"},
    new { id =3, Name = "item-3"},
    new { id =4, Name = "item-4"}
}

And my second list is like this

var secondList = new List<Issue> {
    new Issue{ id =1, Date = "01.01.2017"},
    new Issue{ id =2, Date = "01.02.2017"}
}

So I want to create a new list like this. (same id objects will be isPlanned=true)

var firstList = new List<object> {
    new { id =1, Name = "item-1", isPlanned=true, Date = "01.01.2017"},
    new { id =2, Name = "item-2", isPlanned=true, Date = "01.02.2017"},
    new { id =3, Name = "item-3", isPlanned=false },
    new { id =4, Name = "item-4", isPlanned=false }
}

How can I do this linq functions or lambda operations?

2 Answers

You can use left join for this:

var items = new[] {
    new { id =1, Name = "item-1"},
    new { id =2, Name = "item-2"},
    new { id =3, Name = "item-3"},
    new { id =4, Name = "item-4"}
};
var issues = new[] {
    new { id =1, Date = "01.01.2017"},
    new { id =2, Date = "01.02.2017"}
};
var joined = from item in items
             join issue in issues on item.id equals issue.id into gj
             from sub in gj.DefaultIfEmpty()
             select new { item.id, item.Name, isPlanned=sub?.Date != null, sub?.Date };
foreach (var t in joined) {
    Console.WriteLine("{0} {1} {2} {3}", t.id, t.Name, t.isPlanned, t.Date);
}

The result will have nulls when dates are missing, i.e.

new { id =1, Name = "item-1", isPlanned=true,  Date = "01.01.2017"},
new { id =2, Name = "item-2", isPlanned=true,  Date = "01.02.2017"},
new { id =3, Name = "item-3", isPlanned=false, Date = null },
new { id =4, Name = "item-4", isPlanned=false, Date = null }

Demo.

If you wanted to use a lambda expression, you could do it the following way:

List<Foo> foos = new List<Foo>()
{
    new Foo(){ Id = 1, Name = "item-1"},
    new Foo(){ Id = 2, Name = "item-2"},
    new Foo(){ Id = 3, Name = "item-3"},
    new Foo(){ Id = 4, Name = "item-4"}
};

List<Bar> bars = new List<Bar>()
{
    new Bar(){ Id = 1, Date = "01.01.2017"},
    new Bar(){ Id = 2, Date = "01.02.2017"}
};

IEnumerable<dynamic> result = foos.GroupJoin(bars,
    f => f.Id,
    b => b.Id,
    (foo, bar) => new
    {
        Id = foo.Id,
        Name = foo.Name,
        isPlanned = String.IsNullOrEmpty(bar.SingleOrDefault()?.Date),
        Date = bar.SingleOrDefault()?.Date
    });

here are the classes

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public string Date { get; set; }
}

I noticed also from the comments from another answer that you are not able to use C#6.0, so this section would not work for you

isPlanned = String.IsNullOrEmpty(bar.SingleOrDefault()?.Date),
Date = bar.SingleOrDefault()?.Date

You can replace that with the following:

isPlanned = bar.SingleOrDefault() == null ? false : String.IsNullOrEmpty(bar.SingleOrDefault().Date),
Date = bar.SingleOrDefault() == null ? null : bar.SingleOrDefault().Date
Related