2009-08-17 4 views
1

IEnumerable 함께 가능한 그것의 가능한지 궁금하네요.2 일반 IEnumerators에 가입하는 방법

기본적으로 나는 많은 사용자가있어 데이터베이스에서 콘텐츠를 가져와 검색하고 페이지 할 수 있습니다.

나는이 순간에, SQL 내 코드를 LINQ를 사용하고 있습니다 :

 public IEnumerable<content> allcontent; 

     //Get users friends 
     IEnumerable<relationship> friends = from f in db.relationships 
              where f.userId == int.Parse(userId) 
              select f; 

     IEnumerable<relationship> freindData = friends.ToList(); 

     foreach (relationship r in freindData) 
     { 
      IEnumerable<content> content = from c in db.contents 
              where c.userId == r.userId 
              orderby c.contentDate descending 
              select c; 

     // This is where I need to merge everything together 
     } 

내가 그 어떤 의미를 바랍니다! 매트

+0

모두에게 감사드립니다. – bExplosion

답변

1

당신이 데이터에서 키를 보낼 수, 사용자가보다 2100 친구가 알고 있다면 당신은 이미 쉽게 다시 데이터베이스로로드 : 여기 어떻게됩니까

List<int> friendIds = friendData 
    .Select(r => r.UserId) 
    .Distinct() 
    .ToList(); 

List<content> result = db.contents 
    .Where(c => friendIds.Contains(c.userId)) 
    .ToList(); 

LINQ에가로 각 ID를 변환하는 것입니다 매개 변수를 만들고 IN 절을 작성하여 필터링을 수행합니다. 2100은 SQL 서버가 수용 할 수있는 최대 매개 변수 수입니다 ... 2100 명 이상의 친구가있는 경우 ID 목록을 해체하고 결과 목록을 결합 (연결)해야합니다.


또는, 당신은 당신의 질문에 더 리터럴 대답을 원하는 경우는 - CONCAT 다음 첫 번째에서 항목과 두 번째에서 항목을 반환하는 새로운는 IEnumerable을 만들어 함께 2 IEnumerables을 결합하는 방법입니다.

IEnumerable<content> results = Enumerable.Empty<content>(); 
foreach (relationship r in friendData) 
{ 
    IEnumerable<content> content = GetData(r); 
    results = results.Concat(content); 
} 
+0

고맙습니다. 결국 몇 가지 사항을 변경하고 더 나은 LINQ 문을 작성했지만, 이것은 매우 편리하게 올 것입니다! – bExplosion

0

당신이 내부 조인하고있는 경우, .Intersect() 확장 메서드 봐.

0

어떤 것을 병합 하시겠습니까?

당신이 사용할 수있는 두 가지 옵션이 있습니다 : 당신은 왜 일을하지 마십시오 내가 정확하게 당신이 뭘 하려는지 이해한다면 .SelectMany (...) 또는 .Concat (...)

+0

현재 user1의 콘텐츠를 받고 나서 사용자 2를 얻는 중입니다. 나는 모든 것을 함께 합병하고 싶다. 그래서 사용자 1 + 2 + 3 등으로 끝납니다. – bExplosion

+0

Ah. 당신의 "this is where ..."라는 말을 써서 저를 버렸습니다. (for 루프 안에 있습니다.) –

6

은 :이 당신에게 T이 필드를 RelationshipContent이있는 익명의 유형 인 IEnumerable<T>을 줄 것이다

var result = from r in db.relationships 
      from c in db.contents 
      where r.userId == int.Parse(userId) 
      where c.userId == r.UserId 
      orderby c.contentDate descending 
      select new { 
       Relationship = r, 
       Content = c 
      } 

.

관련 문제