我必须要加入表格,还想使用 lambda 表达式使用 group by 子句

I have to tables I want to join them and also want to use group by clause using lambda expression

下面是 SQL 查询,为此我想编写 lambda 表达式。 Enrollment_Policy 和 Enrollment_MemberPolicy 表的实体模型是 MCEntities。

select mp.PolicyId, p.PolicyNo from 
Enrollment_Policy p inner join Enrollment_MemberPolicy mp 
on mp.PolicyId=p.ID
where mp.UHID=123 group by mp.PolicyId, p.policyNO

我被困在如何编写 group by 子句上。请有人帮助我。

这就是您实现此目标的方法。假设您有 Foo 和 Bar 类:

public class Foo
{
    public int Id { get; set; }
    public string Prop1 { get; set; }
    public int BarId { get; set; }
}

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

那么你可以这样操作它们:

var arr = new List<Foo>() {new Foo(){Id=1, Prop1 = "test"}};
var arr2 = new List<Bar>() {new Bar(){Id=1, Prop2 = "test"}};
var arr3 = arr.Join(arr2, foo => foo.BarId, bar => bar.Id, (foo, bar) => new {foo, bar})
    .Where(t=>t.foo.Prop1=="test")
    .GroupBy(t => new {t.foo.Prop1, t.bar.Prop2});