2012-03-28 38 views
0

我有一个主表水果,我想加入到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 
.... 

要获得每一块的价格水果,我分别加入每个价格表的水果表,然后将结果连接在一起。

如果价格表不能合并成一个,你有更好的方法来解决这个问题吗?例如,构造一个返回所有信息而不是串联多个查询结果的Linq查询。

欣赏你的想法。

回答

0

您需要使用join into,然后DefaultIfEmpty。 SQL LEFT JOIN的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() 

这将给:

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来选择所需的字段。
下面的完整的结果,我用一个匿名类来保存我的价值观:

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分配一个值的语句很快就会看起来很丑。 – 2012-04-11 14:49:47

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

很好................. – 2012-04-14 15:24:49