我正在编写一个LINQ到SQL语句,并且我在c#中使用ON子句的正常内部连接的标准语法之后。

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

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

当前回答

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以获得字段的结果。

其他回答

从d1在dealercontract中加入d2在dealercontract中d1.dealearid = d2.dealerid select new {dealercontract.*}

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 };

学生和课程表在哪里有主键和外键的关系

基本上LINQ连接操作符对SQL没有好处。例如,下面的查询

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

将导致内部连接在SQL

join对IEnumerable<>有用,因为它更有效:

from contact in db.DealerContact  

条款将对每个经销商重新执行 但对于IQueryable<>则不是这样。另外,连接也不太灵活。

使用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
                   };

因为我更喜欢表达式链语法,下面是你如何做到这一点:

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