LINQ to SQL 中内部联接的语法是什么? [英] What is the syntax for an inner join in LINQ to SQL?

查看:39
本文介绍了LINQ to SQL 中内部联接的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 LINQ to SQL 语句,我正在使用 C# 中带有 ON 子句的普通内部连接的标准语法.

I'm writing a LINQ to SQL statement, and I'm after the standard syntax for a normal inner join with an ON clause in C#.

你如何在 LINQ to SQL 中表示以下内容:

How do you represent the following in LINQ to SQL:

select DealerContact.*
from Dealer 
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID

推荐答案

它是这样的:

from t1 in db.Table1
join t2 in db.Table2 on t1.field equals t2.field
select new { t1.field2, t2.field3}

为您的表提供合理的名称和字段作为更好的示例会很好.:)

It would be nice to have sensible names and fields for your tables for a better example. :)

更新

我认为对于您的查询,这可能更合适:

I think for your query this might be more appropriate:

var dealercontacts = from contact in DealerContact
                     join dealer in Dealer on contact.DealerId equals dealer.ID
                     select contact;

因为您正在寻找联系人,而不是经销商.

Since you are looking for the contacts, not the dealers.

这篇关于LINQ to SQL 中内部联接的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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