2015-03-04 2 views
0

Это может показаться глупым, но я трачу достаточно времени, и я думаю, мне нужно больше подвергать linq.Как получить доступ к результатам группы linq

У меня есть выражение linq, которое приводит к группе (key = int, list = string) То, что я хочу, просто. Я хочу присвоить группе список переменной List<string> на основе ключа.

выдержка из SQL, который приводит собой группу

var productPerBranch = 
    from i in 
    (
     from br in 
     (
      from pp in context.ProductPricings 
      join p in productQuery on pp.ProductId equals p.ProductId 
      where organizationRestrictedPartnerLocations.Contains(pp.PartnerLocationId) 
      select new { pp.ProductId, pp.PartnerLocationId } 
     ) 
     join pl in context.PartnerLocation on br.PartnerLocationId equals pl.PartnerLocationId into k 
     from l in k.DefaultIfEmpty() 
     select new { br.ProductId, l.DisplayName } 
    ) 
    group i by i.ProductId into g 
    select g; 

для например, если я хочу, чтобы назначить списки с Key = 0 для этой переменной List <string> l Как я могу это сделать?

ответ

1

Попробуйте это:

var query = 
(
    from br in 
    (
     from pp in context.ProductPricings 
     join p in productQuery on pp.ProductId equals p.ProductId 
     where organizationRestrictedPartnerLocations.Contains(pp.PartnerLocationId) 
     select new { pp.ProductId, pp.PartnerLocationId } 
    ) 
    join pl in context.PartnerLocation on br.PartnerLocationId equals pl.PartnerLocationId into k 
    from l in k.DefaultIfEmpty() 
    select new { br.ProductId, l.DisplayName } 
).ToLookup(x => x.ProductId, x => x.DisplayName); 

var result = query[0].ToList(); 
Смежные вопросы