2012-03-28 7 views
0

저는 메인 테이블 Fruit을 가지고 있으며 테이블 ApplePrice, PearPrice 및 BananaPrice에 가입하고 싶습니다.내부 테이블이 여러 테이블에 동일한 열을 결합합니다

과일

Id Type Date 
-------------------- 
1 Apple 1/1 
2 Apple 1/3 
3 Banana 1/5 
4 Pear 1/7 

공통 분모 [사과/배/바나나] 가격 (각 테이블에 대한 더 많은 특정 필드가) :

Date Price F1 F2 ... 
----------------------- 
1/1  p1 
1/2  p2 
.... 

은 각 부분의 가격을 얻으려면 과일, 나는 각 가격 테이블을 가진 과일 테이블을 따로 따로 결합하고, 그 결과를 함께 연결한다.

Price 테이블을 하나로 병합 할 수없는 경우이 문제에 대한 더 나은 접근 방법이 있습니까? 예를 들어, 여러 쿼리의 결과를 연결하는 대신 모든 정보를 반환하는 하나의 Linq 쿼리를 생성합니다.

당신의 아이디어를 감사하십시오.

답변

0

join into 다음으로 DefaultIfEmpty을 사용해야합니다. SQL LEFT JOIN과 동일한 LINQ입니다.

Fruit | Apple Price | Banana Price | Pear Price 
--------+-------------+--------------+------------ 
    Apple | applePrice |   null |  null 
    Apple | applePrice |   null |  null 
Banana |  null | bananaPrice |  null 
    Pear |  null |   null | pearPrice 

그런 다음 아래로 유효한 fruitPrice 값을 선택합니다 :

applePrice != null 
    ? applePrice.Price 
    : bananaPrice != null 
     ? bananaPrice.Price 
     : pearPrice != null 
      ? pearPrice.Price 
      : 0 // Default value here if all 3 are null 

을 그리고 원하는 필드를 선택하는 LINQ를 사용

from fruit in fruits 
join ap in applePrices 
    on (fruit.Type + fruit.Date.ToShortDateString()) equals ("Apple" + ap.Date.ToShortDateString()) 
    into aps 
from applePrice in aps.DefaultIfEmpty() 

이 제공됩니다.
아래의 완전한 결과는 내 값을 유지하기 위해 익명 클래스를 사용했습니다 :

var prices = from fruit in fruits 
      join ap in applePrices 
       on (fruit.Type + fruit.Date.ToShortDateString()) equals ("Apple" + ap.Date.ToShortDateString()) 
       into aps 
      from applePrice in aps.DefaultIfEmpty() 
      join bp in bananaPrices 
       on (fruit.Type + fruit.Date.ToShortDateString()) equals ("Banana" + bp.Date.ToShortDateString()) 
       into bps 
      from bananaPrice in bps.DefaultIfEmpty() 
      join pp in pearPrices 
       on (fruit.Type + fruit.Date.ToShortDateString()) equals ("Pear" + pp.Date.ToShortDateString()) 
       into pps 
      from pearPrice in pps.DefaultIfEmpty() 
      select new 
       { 
        Id = fruit.Id, 
        Type = fruit.Type, 
        Date = fruit.Date, 
        Price = 
        applePrice != null 
         ? applePrice.Price 
         : bananaPrice != null 
          ? bananaPrice.Price 
          : pearPrice != null 
           ? pearPrice.Price 
           : 0 
       }; 
+0

답변 해 주셔서 감사합니다. 그러나 Price에 값을 할당하는 문장이 매우 추해 보일 것이므로 더 나은 솔루션인지는 확실하지 않습니다. –

0
var prices = from T in bank.students 
         join O in bank.dovres 
         on (T.code) equals 

         (O.codestu) 
         into aps 
         from applePrice in aps.DefaultIfEmpty() 
         join Y in bank.rotbes 
         on (T.code) equals (Y.codestu) 
         into bps 
         from bananaPrice in bps.DefaultIfEmpty() 

         select new 
         { 
          Id = T.code, 
          Type =T.name, 
          Date = T.family, 
          father=T.fathername, 
          T.adi_date, T.faal_date, 
          // = applePrice.sal + " ماه و " + O.mah + " روز", hk = Y.sal + " ماه و " + Y.mah + " روز" 
          hj = applePrice != null 
          ? applePrice.sal + " ماه و " + applePrice.mah + " روز" 
          :"", 
          hj1 = bananaPrice!= null 
          ? bananaPrice.sal + " ماه و " +bananaPrice.mah + " روز" 
          : "", 

         }; 
      dataGridView1.DataSource = prices; 
      dataGridView1.Columns[0].HeaderText = "کد "; 
      dataGridView1.Columns[1].HeaderText = "نام"; 
      dataGridView1.Columns[2].HeaderText = "نام خانوادگی"; 
      dataGridView1.Columns[3].HeaderText = "نام پدر"; 
      dataGridView1.Columns[4].HeaderText = " عضویت عادی"; 
      dataGridView1.Columns[5].HeaderText = "عضویت فعال"; 
      dataGridView1.Columns[6].HeaderText = "کسری بسیج"; 
      dataGridView1.Columns[7].HeaderText = "کسری جبهه"; 

이 3 개 이상의 테이블을 조인에 가장 적합한 코드입니다.

+0

매우 ................. –

관련 문제