在LINQ to SQL中,内连接的语法是什么?

我正在写一条LINQ to SQL语句,我想知道C#中带有`ON'子句的普通内连接的标准语法。

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

select DealerContact.*
from Dealer 
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
对该问题的评论 (5)
解决办法

它是这样的。

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

如果你的表能有合理的名称和字段就更好了,这样的例子就更多了。)

更新

我想对于你的查询,这可能更合适。

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

因为你要找的是联系人,而不是经销商。

评论(1)

因为我更喜欢表达式链语法,所以下面是你如何用它来做。

var dealerContracts = DealerContact.Join(Dealer, 
                                 contact => contact.DealerId,
                                 dealer => dealer.DealerId,
                                 (contact, dealer) => contact);
评论(1)

为了扩展表达式链语法[回答][1],由Clever Human。

如果你想在两个表的字段上做一些事情(比如过滤或选择)--而不是只在这两个表中的一个表上做,你可以在 Join 方法的最终参数的 lambda 表达式中创建一个新的对象,将这两个表整合在一起,比如说。

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表达式。

(dc, d) => new { DealerContact = dc, Dealer = d }

...我们构建了一个新的匿名类型的对象,它的属性是DealerContact和Dealer记录,以及它们的所有字段。

然后我们可以使用这些记录中的字段来筛选和选择结果,就像本例的其余部分所展示的那样,我们使用dc_d作为我们构建的匿名对象的名称,该对象同时拥有DealerContact和Dealer记录作为其属性。

[1]: https://stackoverflow.com/a/3851487/12484

评论(3)
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();
评论(2)

你创建一个外键,LINQ-to-SQL为你创建导航属性。 每个 "Dealer "将有一个 "DealerContacts "的集合,你可以选择、过滤和操作。

from contact in dealer.DealerContacts select contact

context.Dealers.Select(d => d.DealerContacts)

如果你'没有使用导航属性,你就错过了LINQ-to-SQL的一个主要优势--映射对象图的部分。

评论(1)

使用Linq Join操作符。

var q =  from d in Dealer
         join dc in DealerConact on d.DealerID equals dc.DealerID
         select dc;
评论(2)

基本上LINQ的join操作符对SQL没有任何好处。 即 下面的查询

var r = from dealer in db.Dealers
   from contact in db.DealerContact
   where dealer.DealerID == contact.DealerID
   select dealerContact;

在SQL中会产生INNER JOIN

join对IEnumerable<> 因为它的效率更高。

from contact in db.DealerContact  

对每一个都要重新执行该条款。 但对于IQueryable<> 就不是这样了。 另外join也不太灵活。

评论(0)

其实,很多时候最好不要加入,在linq中就是如此。 当有导航属性的时候,一个非常简洁的方法就是写你的linq语句。

from dealer in db.Dealers
from contact in dealer.DealerContacts
select new { whatever you need from dealer or contact }

译为where子句。

SELECT 
FROM Dealer, DealerContact
WHERE Dealer.DealerID = DealerContact.DealerID
评论(2)

使用[LINQ joins][1]执行内联接。

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]: http://msdn.microsoft.com/en-us/library/bb397941.aspx

评论(0)

试试这个......{{5294322}}

     var data =(from t1 in dataContext.Table1 join 
                 t2 in dataContext.Table2 on 
                 t1.field equals t2.field 
                 orderby t1.Id select t1).ToList(); 
评论(0)
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)

试试这个

var dealer = from d in Dealer
             join dc in DealerContact on d.DealerID equals dc.DealerID
             select d;
评论(0)
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 };

在学生表和课程表有主键和外键关系的情况下。

评论(0)

from d1 in DealerContrac join d2 in DealerContrac on d1.dealearid equals d2.dealerid select new {dealercontract.*}。

评论(1)
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();

写出你想要的表名,并初始化选择得到字段的结果。

评论(1)
var data=(from t in db.your tableName(t1) 
          join s in db.yourothertablename(t2) on t1.fieldname equals t2.feldname
          (where condtion)).tolist();
评论(0)
var Data= (from dealer in Dealer join dealercontact in DealerContact on dealer.ID equals dealercontact.DealerID
select new{
dealer.Id,
dealercontact.ContactName

}).ToList();
评论(0)

在linq中对两个表进行内部连接

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 }
评论(0)

一个最好的例子

表名:TBL_EmpTBL_Dep。 "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}
评论(1)