我必须要加入它们的表,并且还想使用lambda表达式使用group by子句

时间:2015-02-04 06:41:40

标签: c# sql-server lambda asp.net-mvc-5

以下是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子句。请有人帮助我。

1 个答案:

答案 0 :(得分:0)

这是你如何实现这一目标的。让我们说你有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});