2013-10-31 4 views
1

이 두 쿼리의 출력 인 combo1과 combo2를 결합하려고합니다. 이 두 linq 쿼리의 출력을 결합하는 방법?

 var combo1 = from c in db.comments 
       join p in db.picture on c.targetpictureid equals p.idpictures 
       join u in db.users on c.iduser equals u.iduser 
       select new TCommentDTO 
       { 

        idcomments=c.idcomments, 
        comment1 = c.comment1, 
        targetpictureid = c.targetpictureid, 
        ctime = c.ctime, 
        iduofpic=p.iduser, 
        iduofcommentor=c.iduser, 
        profilepicofcommentor=u.profilepic, 
        usernameofcommentor=u.username, 
        picFilename=p.picFilename, 
        picTitle=p.picTitle 


       }; 

     var combo2 = from f in db.followers 
         join u in db.users on f.iduser equals u.iduser 
        select new TfollowerDTO 
        { 
         idfollowers = f.idfollowers, 
         iduser = f.iduser, 
         targetiduser = f.targetiduser, 
         startedfollowing = f.startedfollowing, 
         unoffollower = u.username, 
         ppoffollower = u.profilepic, 
         status = u.status 


        }; 

     var resultantcombo =combo1.Union(combo2); 
     return resultantcombo; 

그러나 두 번째 마지막 줄에

은 내가 이러한 오류를 얻고, 노동 조합을 수행하고있다 : 지금 당으로 내가 이런 코드 뭔가 쓸 수 있어요

오류 1 암시 할 수 없습니다 'System.Collections.Generic.IEnumerable'을 'System.Linq.IQueryable'로 변환하십시오. 명시 적 변환 (? 당신이 캐스트를 누락)

오류 2 인스턴스 인수 존재 'System.Linq.ParallelQuery'

오류 3 '시스템>에>'System.Linq.IQueryable '에서 변환 할 수 없습니다 .Linq.IQueryable 'Union'에 대한 정의 및 최상의 확장 메서드 오버로드가 없습니다. 'System.Linq.ParallelEnumerable.Union (System.Linq.ParallelQuery, System.Collections.Generic.IEnumerable)'에 잘못된 인수가 있습니다.

내가 틀린데이 두 쿼리를 성공적으로 결합하려면 어떻게해야합니까?

+1

무엇 출력의 종류는 당신이 기대하는 - 당신은 두 가지 유형의 컬렉션을 결합하고 있습니까? – Paddy

+0

내가 막 말하고 싶었지만 어쩌면 나는 무식하지만 Union의 두 가지 유형의 콜렉션을 할 수 없습니까? – dgarbacz

+0

그래, 네가 노조를 할 수 없다는 걸 알기 때문에 다른 길은 어떨까? – Obvious

답변

3

당신은 익명의 유형 반환 할 수 있습니다 : 당신이 방법에서 매개 변수로 주위를 전달하려는 경우에 당신은 속성으로 두 개의 컬렉션을 포함하는 클래스를 만들어야합니다,

return Json(new { 
    Comments = combo1, 
    Followers = combo2 
}); 

을 :

public class CommentsAndFollowersDTO 
{ 
    public IEnumerable<TCommentDTO> Comments { get; set; } 

    public IEnumerable<TfollowerDTO> Followers { get; set; } 
} 
0

나는 두 가지 유형의 조합을 조합 할 수 없다고 생각합니다. 당신이 재산을 가진 새로운 클래스를 만들어보고하지 않는 경우 당신은 항상 두 개의 쿼리를 가지고 내가 원하는 출력이 무엇인지 확실하지 않다 그래서

var q = from c1 in combo1 
     from c2 in combo2 
     select new { 
      Comments = c1, 
      Followers = c2 
     } 

처럼 익명의 객체로 가입 할 수 있지만, 이 두 가지 방법을 모두 사용하면 효과적입니다.

관련 문제