在实体框架中将行转换为列

时间:2018-07-17 06:37:33

标签: c# entity-framework linq lambda entity-framework-4

如何在实体框架中将行转换为列!?

我得到这样的结果:

the result my code

我想要这个结果:

i want this result

这是我的实体代码:

window.onunload

并且行数始终小于3,我希望有3列。

请告诉我您的答案。 坦克。

1 个答案:

答案 0 :(得分:1)

此查询将仅返回一行,在这种情况下,waranterIds将包含三个WarranterPersonID值,该字段也是List<int>类型的,因为它的数量不是在编译时已知:

var answer = (from loanPerson in context.LoanPersons.Where(x => x.Id == 84829)
              join warranter in context.Warranters 
              on loanPerson.Id equals warranter.LoanPersonId
              group warranter by loanPerson.Id into sub
              select new
              {
                 loanPersonId = sub.Key,
                 waranterIds = sub.Select(x => x.LoanPersonId).ToList()

                 //if you sure, that quantity equals 3, 
                 //you can write this code instead of waranterIds:
                 //zamen1 = sub.Select(x => x.LoanPersonId).First(),
                 //zamen2 = sub.Select(x => x.LoanPersonId).Skip(1).First(),
                 //zamen3 = sub.Select(x => x.LoanPersonId).Skip(2).First()
              }).ToList();
相关问题