What is the syntax for an inner join in LINQ to SQL?
我在写一条linq-to-sql语句,我在用C中的
如何在Linq to SQL中表示以下内容:
1 2 3 | select DealerContact.* from Dealer inner join DealerContact on Dealer.DealerID = DealerContact.DealerID |
它是这样的:
1 2 3 | from t1 in db.Table1 join t2 in db.Table2 on t1.field equals t2.field select new { t1.field2, t2.field3} |
最好为您的表提供合理的名称和字段,以获得更好的示例。:)
更新
我认为对于您的查询,这可能更合适:
1 2 3 | var dealercontacts = from contact in DealerContact join dealer in Dealer on contact.DealerId equals dealer.ID select contact; |
因为你在找联系人,而不是经销商。
因为我更喜欢表达式链语法,下面介绍如何使用它:
1 2 3 4 | var dealerContracts = DealerContact.Join(Dealer, contact => contact.DealerId, dealer => dealer.DealerId, (contact, dealer) => contact); |
要通过聪明的人扩展表达式链语法答案:
如果要对两个被联接在一起的表中的字段执行操作(如筛选或选择),而只对这两个表中的一个表执行操作,则可以在联接方法的最后一个参数lambda表达式中创建一个新对象,该方法合并了这两个表,例如:
1 2 3 4 5 6 7 8 9 10 11 12 | var dealerInfo = DealerContact.Join(Dealer, dc => dc.DealerId, d => d.DealerId, (dc, d) => new { DealerContact = dc, Dealer = d }) .Where(dc_d => dc_d.Dealer.FirstName =="Glenn" && dc_d.DealerContact.City =="Chicago") .Select(dc_d => new { dc_d.Dealer.DealerID, dc_d.Dealer.FirstName, dc_d.Dealer.LastName, dc_d.DealerContact.City, dc_d.DealerContact.State }); |
有趣的部分是该示例第4行中的lambda表达式:
1 |
…在这里,我们构造一个新的匿名类型对象,该对象的属性包括dealerContact和dealer记录及其所有字段。
然后,我们可以在筛选和选择结果时使用这些记录中的字段,如示例的其余部分所示,该示例使用
1 2 3 4 5 6 7 8 9 | var results = from c in db.Companies join cn in db.Countries on c.CountryID equals cn.ID join ct in db.Cities on c.CityID equals ct.ID join sect in db.Sectors on c.SectorID equals sect.ID where (c.CountryID == cn.ID) && (c.CityID == ct.ID) && (c.SectorID == company.SectorID) && (company.SectorID == sect.ID) select new { country = cn.Name, city = ct.Name, c.ID, c.Name, c.Address1, c.Address2, c.Address3, c.CountryID, c.CityID, c.Region, c.PostCode, c.Telephone, c.Website, c.SectorID, Status = (ContactStatus)c.StatusID, sector = sect.Name }; return results.ToList(); |
使用LINQ联接运算符:
1 2 3 | var q = from d in Dealer join dc in DealerConact on d.DealerID equals dc.DealerID select dc; |
创建一个外键,Linq to SQL为您创建导航属性。然后,每个
1 | from contact in dealer.DealerContacts select contact |
或
1 | context.Dealers.Select(d => d.DealerContacts) |
如果您不使用导航属性,那么您就错过了LinqtoSQL的一个主要优点——映射对象图的部分。
基本上,linq join操作符对SQL没有好处。即以下查询
1 2 3 4 | var r = from dealer in db.Dealers from contact in db.DealerContact where dealer.DealerID == contact.DealerID select dealerContact; |
将导致SQL中的内部联接
联接对于IEnumerable<>很有用,因为它更有效:
1 | from contact in db.DealerContact |
条款将针对每个经销商重新执行。但对于iqueryable来说,情况并非如此。加入也不那么灵活。
实际上,通常最好不要加入,也就是说,加入Linq。当存在导航属性时,编写LINQ语句的一种非常简洁的方法是:
1 2 3 | from dealer in db.Dealers from contact in dealer.DealerContacts select new { whatever you need from dealer or contact } |
它翻译成一个WHERE子句:
1 2 3 | SELECT <columns> FROM Dealer, DealerContact WHERE Dealer.DealerID = DealerContact.DealerID |
使用LINQ联接执行内部联接。
1 2 3 4 5 6 7 8 9 | 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 }; |
试试这个:
1 2 3 4 | var data =(from t1 in dataContext.Table1 join t2 in dataContext.Table2 on t1.field equals t2.field orderby t1.Id select t1).ToList(); |
1 2 3 4 5 6 7 | var q=(from pd in dataContext.tblProducts join od in dataContext.tblOrders on pd.ProductID equals od.ProductID orderby od.OrderID select new { od.OrderID, pd.ProductID, pd.Name, pd.UnitPrice, od.Quantity, od.Price, }).ToList(); |
1 2 3 4 5 | OperationDataContext odDataContext = new OperationDataContext(); var studentInfo = from student in odDataContext.STUDENTs join course in odDataContext.COURSEs on student.course_id equals course.course_id select new { student.student_name, student.student_city, course.course_name, course.course_desc }; |
学生和课程表之间有主键和外键关系
试试这个,
1 2 3 | var dealer = from d in Dealer join dc in DealerContact on d.DealerID equals dc.DealerID select d; |
1 2 3 4 5 6 | var Data= (from dealer in Dealer join dealercontact in DealerContact on dealer.ID equals dealercontact.DealerID select new{ dealer.Id, dealercontact.ContactName }).ToList(); |
1 2 3 | var data=(from t in db.your tableName(t1) join s in db.yourothertablename(t2) on t1.fieldname equals t2.feldname (where condtion)).tolist(); |
1 2 | var list = (from u in db.Users join c in db.Customers on u.CustomerId equals c.CustomerId where u.Username == username select new {u.UserId, u.CustomerId, u.ClientId, u.RoleId, u.Username, u.Email, u.Password, u.Salt, u.Hint1, u.Hint2, u.Hint3, u.Locked, u.Active,c.ProfilePic}).First(); |
编写所需的表名,并初始化select以获取字段的结果。
LINQ C中的内部联接两个表#
1 2 3 4 | var result = from q1 in table1 join q2 in table2 on q1.Customer_Id equals q2.Customer_Id select new { q1.Name, q1.Mobile, q2.Purchase, q2.Dates } |
一个最好的例子
表名:
1 2 3 4 5 6 7 8 9 10 11 | 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} |