2008-10-10 2 views
1

내 저장소에 해당지도가 있습니다. 서비스에서스칼라 값이 아닌 LinqToSql 그룹에 쿼리로 그룹을 작성하는 방법이 있습니까?

public IQueryable<AwType> GetAwTypes() 
{ 
    return from awt in _db.AwTypes 
      select new AwType 
      { 
       Id = awt.Id, 
       Header = awt.Header, 
       Description = awt.Description 
      }; 
} 

public IQueryable<Aw> GetAws() 
{ 
    return from aw in _db.Aws 
      select new Aw 
      { 
       Id = aw.Id, 
       Bw = (from bw in GetBws() 
          where bw.Id == aw.Bw 
          select bw 
         ).SingleOrDefault(), 
       AwType = (from awt in GetAwTypes() 
          where awt.Id == awAwType 
          select awt 
         ).SingleOrDefault(), 
       AwAttribute = aw.AwAttribute 
      }; 
} 

나는 List<KeyValuePair<AwType, int>>로 AwType별로 그룹화 신주 인수권 부사채의 수를 싶어. 내가 그 LINQ 쿼리 호출 할 때 는 :

var awGroups = from aw in _repository.GetAws() 
group aw by aw.AwType into newGroup 
select newGroup; 

List<KeyValuePair<AwType, int>> RetGroups = new List<KeyValuePair<AwType, int>>(); 
foreach (var group in awGroups) 
{ 
    RetGroups.Add(new KeyValuePair<AwType, int>(group.Key, group.Count())); 
} 
return RetGroups; 

을 나는 aw.AwType.Id 같은 스칼라 값 그룹에있는 객체에 의해 내가 할 수없는 그룹을 말하는 오류가 발생합니다.

한 번의 호출로 "AwType, int"쌍을 얻을 수있는 방법이 있습니까?

답변

1

AwType가 참조 형에 의해 그룹. 해당 참조 유형을 그룹화하는 것은 좋지 않습니다. 해당 쿼리의 각 AwType은 고유 한 참조이므로 n 개의 요소가 n 개의 그룹을 생성합니다.

내가 LINQ의 이해가 무엇에서
var awGroups = from aw in _repository.GetAws() 
group aw by aw.AwType.ID into newGroup //changed to group on ID 
select newGroup; 

List<KeyValuePair<AwType, int>> RetGroups = new List<KeyValuePair<AwType, int>>(); 
foreach (var group in awGroups) 
{ 
    //changed to get the first element of the group and examine its AwType 
    RetGroups.Add(new KeyValuePair<AwType, int>(group.First().AwType, group.Count())); 
} 
return RetGroups; 
+0

감사 무슨 뜻인지 이해하지 못했다 새로운 {푸, 바 ...}에 의해 그룹 아 당신을 의미합니까, 나는 거의 ... 대답을 기다리고 포기했습니다 :) – spinodal

0

할 수 있습니다 익명의 유형, 예를 들어, 새로운 {푸, 바}

+0

난 당신이 ... – spinodal

0

, 당신이 테이블의 모든 열을 기준으로 그룹에 노력하는 것과 같습니다

이 시도해보십시오. 이것은

var awGroups = from aw in _repository.GetAws() 
group aw by aw.AwType.Id, aw.AwType.Header, aw.AwType.Description into newGroup 
select newGroup; 

아니면 (내가 Linq에 대해 매우 지식이 아니에요) 그룹 만 문에서 테이블에있는 모든 단일 필드를 나열하여 수행 할 수 있습니다, 그래서 귀하의 경우, 다음을 수행해야합니다 그것이 필요한 유일한 컬럼 인 경우 ID로 그룹화 할 수 있습니다.

var awGroups = from aw in _repository.GetAws() 
group aw by aw.AwType.Id into newGroup 
select newGroup; 
관련 문제