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

convert rows to column in entity framwork

如何将 entity framework 中的行转换为列!?

我有这样的结果:

我想要这个结果:

我的实体代码是这样的:

(from loanPerson in context.LoanPersons.AsParallel()
                  join warranter in context.Warranters.AsParallel() on loanPerson.Id equals warranter.LoanPersonId
                  where loanPerson.Id == 84829
                  select new
                  {
                      loanPersonId = loanPerson.Id,
                      waranterId = warranter.WarranterPersonID,
                  }).ToList();

行数总是小于 3,我想要 3 列。

请告诉我你的答案。 坦克.

此查询将 return 只有一行,其中 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();