简体   繁体   中英

convert rows to column in entity framwork

how can i convert rows to column in entity framework!?

i have a result like this:

结果我的代码

and i want this result:

我想要这个结果

my entity code i this :

(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();

and number of the row always less than 3 and i want to have 3 column.

please let me know your answer. tanks.

This query will return the only one row, where waranterIds will contain, at this particular case, three WarranterPersonID values, also this field is of List<int> type, because it's quantity not known at compile time:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM