Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Lambda Join, with 2 join fields or more error

I'm new to C#, I try to Groupjoin with more than one fields as the keys using LINQ using Extension methods but with no success.

My Class

public class NewStruct
    {
        public string newId { get; set; }
        public IEnumerable<Table2> newTable2 { get; set; }
    }

My Code

var context = new MyDBContex();

IQueryable<NewStruct> list = context.Table1
    .GroupJoin(context.Table2,
        e => new { e.Id1, e.Id2, e.Datefield },
        q => new {q.Id1, q.Id2, e.Datefield },
    (t1, t2) => new NewStruct { newId= t1.Id1, newTable2 = t2 });

this will raise error

Error   CS0411  The type arguments for method 'Queryable.GroupJoin<TOuter, TInner, TKey, TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, IEnumerable<TInner>, TResult>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

but, when the condition fild for joining is only one like this

IQueryable<NewStruct> list = context.Table1
    .GroupJoin(context.Table2,
        e => e.Id1,
        q => q.Id1,
    (t1, t2) => new NewStruct { newId= t1.Id1, newTable2 = t2 });

it's no errors

What did I do wrong?

Thanks in advance

like image 939
zie Avatar asked Dec 02 '25 13:12

zie


1 Answers

I have found the work around, the code should like this,

IQueryable<NewStruct> list = context.Table1
    .GroupJoin(context.Table2,
        e => new { id1 = e.Id1, id2 = e.Id2, dt = e.Datefield },
        q => new { id1 = q.Id1, id2 = q.Id2, dt = e.Datefield },
    (t1, t2) => new NewStruct { newId= t1.Id1, newTable2 = t2 });

thank you all

like image 64
zie Avatar answered Dec 04 '25 06:12

zie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!