LINQ查询加入4桌 [英] Linq query to join 4 tables

查看:76
本文介绍了LINQ查询加入4桌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列编号,shiftid,发生,delfee)的表称为交付。我有三个其他表delcash,delcc,delsplit。德尔变现列(ID,成本,支付),delcc有列(ID,cccost,ccpaid,cashtip),delsplit有列(ID,cashcost,cashpaid,cccost,ccpaid)。该ID的交付项链接的基础上支付的类型,其他三个表之一。我试图让那里的表的方式连接在一起,这样我就可以遍历List,并显示在出现的顺序每次交付以及每次交付的尖端和交货费观统领表列表。任何想法,如果能够做到这一点?或者我应该只使用SQL?

  SELECT deliveries.occurrence,deliveries.delfee,delcash.cost,delcash.paid,delcc.cost,delcc.paid,delcc.cashtip,
delsplit.cashcost,delsplit.cashpaid,delsplit.cccost,delsplit.ccpaid
从交付
LEFT JOIN delcash
ON deliveries.id = delcash.id
LEFT JOIN delcc
ON deliveries.id = delcc.id
LEFT JOIN delsplit
ON deliveries.id = delsplit.id


解决方案

尝试是这样的:

  VAR joinedDel从交付tblDeliveries =
                加入DELC在tblDelCash上delivery.id等于delC.id
                加入delCC在tblDelCreditCard上delivery.id等于delCC.id
                加入delSplit在tblDelSplit上delivery.id等于delSplit.id
                选择新{
                      //然后创建匿名对象的新实例,您将设置其属性的所有值
                      //例如,您可以设置ShiftID = delivery.ShiftID
                };

我也建议首先一定要规范你的表(见供你参考这个链接)。这会给你你如何将正确地加入这些表的详细了解。

I have a table called deliveries with columns id,shiftid, occurrence,delfee). I have three other tables delcash,delcc,delsplit. Del cash has columns (id,cost,paid), delcc has columns (id,cccost,ccpaid,cashtip), delsplit has columns(id,cashcost,cashpaid,cccost,ccpaid). The id links the delivery entries to one of the three other tables based on the type of payment. I am trying to get a list where the tables are joined together in a way so i can iterate through a list and get a table in a view that shows each delivery in order of occurrence as well as the tip and delivery fee of each delivery. Any idea if it is possible to do this? Or should I just use SQL?

SELECT deliveries.occurrence, deliveries.delfee,delcash.cost,delcash.paid,delcc.cost,delcc.paid,delcc.cashtip,
delsplit.cashcost,delsplit.cashpaid,delsplit.cccost,delsplit.ccpaid
FROM deliveries
LEFT JOIN delcash
ON deliveries.id = delcash.id
LEFT JOIN delcc
ON deliveries.id = delcc.id
LEFT JOIN delsplit
ON deliveries.id = delsplit.id

解决方案

Try something like this:

var joinedDel = from delivery in tblDeliveries
                join delC in tblDelCash on delivery.id equals delC.id
                join delCC in tblDelCreditCard on delivery.id equals delCC.id
                join delSplit in tblDelSplit on delivery.id equals delSplit.id
                select new {
                      //Then create a new instance of Anonymous object where you will set all values of its properties
                      //For example, you can set ShiftID = delivery.ShiftID
                };

I will also suggest to normalize first your tables(see this link for your reference) . This will give you more understanding on how will you join those tables properly.

这篇关于LINQ查询加入4桌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆