我正在编写一个LINQ到SQL语句,并且我在c#中使用ON子句的正常内部连接的标准语法之后。
如何在LINQ to SQL中表示以下内容:
select DealerContact.*
from Dealer
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
我正在编写一个LINQ到SQL语句,并且我在c#中使用ON子句的正常内部连接的标准语法之后。
如何在LINQ to SQL中表示以下内容:
select DealerContact.*
from Dealer
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
当前回答
使用LINQ连接执行内部连接。
var employeeInfo = from emp in db.Employees
join dept in db.Departments
on emp.Eid equals dept.Eid
select new
{
emp.Ename,
dept.Dname,
emp.Elocation
};
其他回答
使用Linq Join操作符:
var q = from d in Dealer
join dc in DealerConact on d.DealerID equals dc.DealerID
select dc;
创建一个外键,LINQ-to-SQL为您创建导航属性。然后每个Dealer都有一个DealerContacts集合,您可以选择、筛选和操作这些DealerContacts。
from contact in dealer.DealerContacts select contact
or
context.Dealers.Select(d => d.DealerContacts)
如果不使用导航属性,就会错过LINQ-to-SQL的一个主要好处——映射对象图的部分。
试试这个:
var data =(from t1 in dataContext.Table1 join
t2 in dataContext.Table2 on
t1.field equals t2.field
orderby t1.Id select t1).ToList();
一个最好的例子
表名:TBL_Emp和TBL_Dep
var result = from emp in TBL_Emp join dep in TBL_Dep on emp.id=dep.id
select new
{
emp.Name;
emp.Address
dep.Department_Name
}
foreach(char item in result)
{ // to do}
试试这个吧,
var dealer = from d in Dealer
join dc in DealerContact on d.DealerID equals dc.DealerID
select d;