我将使用示例解释我的问题。
假设我有linq
个查询结果。
var result1=from c in client
select new my_type
{
...
stockDesctiption=??
};
我们会说Client
已提交名为 stockId 的文件。这与dammadgeStockHistory
表中的 stockId 相同。这些表在任何外键约束中都没有连接。
如果我需要为每个客户端获取dammadgeStockHistory.stockDescrption
如何执行此操作。
答案 0 :(得分:1)
通过此字段连接表(不需要外键约束):
var result1 = from c in client
join dsh in dammadgeStockHistory
on c.stockId equal dsh.stockId
select new my_type
{
stockId = c.stockId,
// ...
stockDesctiption = dsh.stockDescrption
};
更新如果你想做'左连接':
var result1 = from c in client
join dsh in dammadgeStockHistory
on c.stockId equal dsh.stockId into g
from cdsh in g.DefaultIfEmpty()
select new my_type
{
stockId = c.stockId,
// ...
stockDesctiption = cdsh == null ? null : dsh.stockDescrption
};