我是C#的新手,我尝试使用LINQ(使用扩展方法)使用多个字段作为关键字进行Groupjoin,但没有成功。
我的班级
public class NewStruct
{
public string newId { get; set; }
public IEnumerable<Table2> newTable2 { get; set; }
}
我的代码
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 });
这会引发错误
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.
但是,当要加入的条件仅是这样的情况
IQueryable<NewStruct> list = context.Table1
.GroupJoin(context.Table2,
e => e.Id1,
q => q.Id1,
(t1, t2) => new NewStruct { newId= t1.Id1, newTable2 = t2 });
没有错误
我做错了什么?
预先感谢
答案 0 :(得分:0)
我找到了解决方法,代码应该像这样
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 });
谢谢大家