[SQL] 내부는 SQL에 LINQ에서 조인 구문은 무엇입니까?
SQL내부는 SQL에 LINQ에서 조인 구문은 무엇입니까?
나는 SQL 문에 LINQ를 쓰고 있어요, 그리고 정상적인 내부의 표준 구문은 C #에서 온 절에 가입 후 난.
어떻게 SQL에 LINQ에 다음을 표시합니까 :
select DealerContact.*
from Dealer
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
해결법
-
==============================
1.이 같은 간다 :
이 같은 간다 :
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;
당신이 접촉하지 딜러를 찾고입니다.
-
==============================
2.내가 표현 체인 구문을 선호하기 때문에, 여기에 당신이 그와 함께 할 방법은 다음과 같습니다
내가 표현 체인 구문을 선호하기 때문에, 여기에 당신이 그와 함께 할 방법은 다음과 같습니다
var dealerContracts = DealerContact.Join(Dealer, contact => contact.DealerId, dealer => dealer.DealerId, (contact, dealer) => contact);
-
==============================
3.영리한 인간에 의해 발현 체인 구문 응답을 확장 :
영리한 인간에 의해 발현 체인 구문 응답을 확장 :
대신 두 테이블 중 하나에 - - 두 테이블이 함께 결합되는이 분야에 (필터처럼 또는 선택) 일을하고 싶었다면 당신은 가입 메소드의 마지막 매개 변수의 람다 식에 새 개체를 만들 수 있습니다 예를 들어, 해당 테이블을 모두 통합 :
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)의 람다 식이다 :
(dc, d) => new { DealerContact = dc, Dealer = d }
... 여기서 우리가 그들의 모든 필드와 함께, 속성으로 DealerContact 및 딜러 레코드가 새로운 익명 타입의 객체를 생성.
우리는 필터링하고 그 결과를 선택할 때 사용이 속성으로 DealerContact 및 판매점 기록을 모두 가지고 우리가 내장 된 익명 객체의 이름으로 dc_d 예제의 나머지에 의해 입증 된 바와 같이 우리는 그 다음 레코드의 필드를 사용할 수 있습니다.
-
==============================
4.
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();
-
==============================
5.사용 Linq는 운영자에 가입 :
사용 Linq는 운영자에 가입 :
var q = from d in Dealer join dc in DealerConact on d.DealerID equals dc.DealerID select dc;
-
==============================
6.당신은 외래 키를 생성하고 LINQ --SQL에 당신을위한 탐색 속성을 만듭니다. 각 판매점 그런 다음, 선택 필터 및 조작 할 수 DealerContacts의 컬렉션을해야합니다.
당신은 외래 키를 생성하고 LINQ --SQL에 당신을위한 탐색 속성을 만듭니다. 각 판매점 그런 다음, 선택 필터 및 조작 할 수 DealerContacts의 컬렉션을해야합니다.
from contact in dealer.DealerContacts select contact
또는
context.Dealers.Select(d => d.DealerContacts)
객체 그래프를 매핑하는 부분 - 당신이 탐색 속성을 사용하지 않는 경우 LINQ - 투 - SQL의 주요 이점 중 하나를 놓치고 있습니다.
-
==============================
7.기본적으로 LINQ 연산자는 SQL에 대한 혜택을 제공하지 않는다 가입 할 수 있습니다. 즉 다음 쿼리
기본적으로 LINQ 연산자는 SQL에 대한 혜택을 제공하지 않는다 가입 할 수 있습니다. 즉 다음 쿼리
var r = from dealer in db.Dealers from contact in db.DealerContact where dealer.DealerID == contact.DealerID select dealerContact;
INNER 발생합니다 SQL에 가입하세요
그것이 더 효율적이기 때문에 조인을 IEnumerable <>에 유용합니다 :
from contact in db.DealerContact
절은 모든 상인을 위해 다시 실행된다 그러나 된 IQueryable <대한> 그것은 사실이 아니다. 또한 가입 덜 유연하다.
-
==============================
8.사실, 자주는 LINQ에 더 가입 할 수 없습니다. 탐색 속성에게 LINQ 문을 작성하는 매우 간결한 방법이있는 경우입니다 :
사실, 자주는 LINQ에 더 가입 할 수 없습니다. 탐색 속성에게 LINQ 문을 작성하는 매우 간결한 방법이있는 경우입니다 :
from dealer in db.Dealers from contact in dealer.DealerContacts select new { whatever you need from dealer or contact }
그것은 어디 절에 변환 :
SELECT <columns> FROM Dealer, DealerContact WHERE Dealer.DealerID = DealerContact.DealerID
-
==============================
9.사용 LINQ는 내부 조인 수행하기 위해 섭니다.
사용 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 };
-
==============================
10.이 시도 :
이 시도 :
var data =(from t1 in dataContext.Table1 join t2 in dataContext.Table2 on t1.field equals t2.field orderby t1.Id select t1).ToList();
-
==============================
11.
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();
-
==============================
12.
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 };
어디 학생 물론 테이블이 기본 키와 외래 키 관계가
-
==============================
13.대신이 시도
대신이 시도
var dealer = from d in Dealer join dc in DealerContact on d.DealerID equals dc.DealerID select d;
-
==============================
14.
var Data= (from dealer in Dealer join dealercontact in DealerContact on dealer.ID equals dealercontact.DealerID select new{ dealer.Id, dealercontact.ContactName }).ToList();
-
==============================
15.
var data=(from t in db.your tableName(t1) join s in db.yourothertablename(t2) on t1.fieldname equals t2.feldname (where condtion)).tolist();
-
==============================
16.
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();
쓰기 테이블 이름은 당신이 원하는, 그리고 필드의 결과를 얻기 위해 선택을 초기화합니다.
-
==============================
17.내부는 LINQ C #에서 두 테이블을 조인
내부는 LINQ C #에서 두 테이블을 조인
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 }
-
==============================
18.d1.dealearid의 등호에 DealerContrac에 D2에 가입 DealerContrac에서 D1에서 새로운 {dealercontract합니다. *} 선택 d2.dealerid
d1.dealearid의 등호에 DealerContrac에 D2에 가입 DealerContrac에서 D1에서 새로운 {dealercontract합니다. *} 선택 d2.dealerid
-
==============================
19.하나의 좋은 예
하나의 좋은 예
테이블 이름 : 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}
from https://stackoverflow.com/questions/37324/what-is-the-syntax-for-an-inner-join-in-linq-to-sql by cc-by-sa and MIT license
'SQL' 카테고리의 다른 글
[SQL] CASE 및 GROUP BY와 피벗에 동적 대안 (0) | 2020.03.11 |
---|---|
[SQL] SQL가 null = NULL [중복] (0) | 2020.03.10 |
[SQL] 어떻게 날짜의 범위와 테이블을 채우는? (0) | 2020.03.10 |
[SQL] 왜 NULL = NULL은 SQL 서버에서 false로 평가 않습니다 (0) | 2020.03.10 |
[SQL] MySQL의 LIKE IN ()? (0) | 2020.03.10 |