内部将表的同一列连接到多个表

时间:2012-03-28 18:49:30

标签: linq

我有一个主表Fruit,我想加入ApplePrice,PearPrice和BananaPrice表。

水果

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

[Apple / Pear / Banana]价格的共同分母(每张表有更多特定字段):

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

为了获得每件Fruit的价格,我将Fruit表与每个价格表分开加入,然后将结果连接在一起。

如果价格表无法合并为一个,您是否有更好的方法解决此问题?例如,构造一个Linq查询,该查询返回所有信息,而不是连接多个查询的结果。

欣赏您的想法。

2 个答案:

答案 0 :(得分:0)

您需要使用join into然后DefaultIfEmpty。 LINQ等效于SQL LEFT JOIN。

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
                 };

答案 1 :(得分: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个或更多表格的最佳代码。