Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 将表的同一列内部联接到多个表_Linq - Fatal编程技术网

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

Linq 将表的同一列内部联接到多个表,linq,Linq,我有一个主要的餐桌水果,我想把它加入到苹果价格、梨价格和香蕉价格的餐桌上 果 Id Type Date -------------------- 1 Apple 1/1 2 Apple 1/3 3 Banana 1/5 4 Pear 1/7 [苹果/梨/香蕉]价格的公分母(每个表有许多更具体的字段): 为了得到每个水果的价格,我将水果表与每个价格表分别连接起来,然后将结果连接在一起 如果价格表不能合并成一个,你有更好的方法解决

我有一个主要的餐桌水果,我想把它加入到苹果价格、梨价格和香蕉价格的餐桌上

Id    Type    Date  
--------------------  
 1    Apple   1/1
 2    Apple   1/3
 3    Banana  1/5
 4    Pear    1/7
[苹果/梨/香蕉]价格的公分母(每个表有许多更具体的字段):

为了得到每个水果的价格,我将水果表与每个价格表分别连接起来,然后将结果连接在一起

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


感谢你的想法

您需要使用
加入
然后使用
DefaultIfEmpty
。相当于SQL左联接的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
然后通过以下选项选择有效的价格值:

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

这是加入3个或更多表的最佳代码。

谢谢您的回答。不过,我不确定这是否是一个更好的解决方案,因为为价格赋值的声明很快就会变得难看。
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
                 };
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 = "کسری جبهه";