LINQ에서 SQL로의 내부 결합 구문은 무엇입니까?
SQL에 대한 LINQ 문을 쓰고 있는데, 일반적인 내부 조인에 대한 표준 구문을 찾고 있습니다.ONC#의 절입니다.
LINQ to SQL에서 다음을 어떻게 표현합니까?
select DealerContact.*
from Dealer
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
다음과 같은 내용입니다.
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;
딜러가 아니라 연락처를 찾고 계시기 때문에
표현식 체인 구문을 선호하기 때문에, 그 방법은 다음과 같습니다.
var dealerContracts = DealerContact.Join(Dealer,
contact => contact.DealerId,
dealer => dealer.DealerId,
(contact, dealer) => contact);
Clear Human 식 체인 구문을 확장하려면 다음 절차를 수행합니다.
두 테이블 중 하나만 결합하는 대신 두 테이블에서 필드(필터 또는 선택 등)를 수행할 경우 두 테이블을 모두 포함하는 Join 메서드에 대한 최종 매개변수의 lamda 식에 새 개체를 만들 수 있습니다. 예를 들어 다음과 같습니다.
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 }
...여기서 우리는 딜러 연락처 및 딜러점의 모든 필드와 함께 속성으로 기록되는 새로운 익명 유형의 개체를 구성합니다.
그런 다음 이러한 레코드의 필드를 사용하여 필터링하고 결과를 선택할 수 있습니다.예의 나머지 부분에서 보여지듯이 다음과 같이 합니다.dc_dDell이 작성한 익명 객체의 이름으로, 이 객체의 자산에는 딜러 연락처 및 딜러 레코드가 모두 포함되어 있습니다.
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-to-SQL이 탐색 속성을 생성합니다.각각Dealer그 후, 의 컬렉션을 갖게 됩니다.DealerContacts선택, 필터링 및 조작이 가능합니다.
from contact in dealer.DealerContacts select contact
또는
context.Dealers.Select(d => d.DealerContacts)
탐색 속성을 사용하지 않는 경우 LINQ-to-SQL의 주요 이점 중 하나인 객체 그래프를 매핑하는 부분이 누락됩니다.
Linq Join 연산자 사용:
var q = from d in Dealer
join dc in DealerConact on d.DealerID equals dc.DealerID
select dc;
기본적으로 LINQ join 연산자는 SQL에 이점을 제공하지 않습니다. 즉, 다음 쿼리
var r = from dealer in db.Dealers
from contact in db.DealerContact
where dealer.DealerID == contact.DealerID
select dealerContact;
SQL에서 INSER JOIN이 됩니다.
join은 IEnumerable << 고객명 >>에게 도움이 됩니다.이것은, 보다 효율적이기 때문입니다.
from contact in db.DealerContact
조항은 모든 딜러에 대해 재실행되지만 IQueryable의 경우는 그렇지 않습니다.또한 조인(join)은 유연성이 떨어집니다.
사실 linq에서는 가입하지 않는 것이 좋은 경우가 많습니다.네비게이션 속성이 있는 경우 linq 문을 쓰는 매우 간단한 방법은 다음과 같습니다.
from dealer in db.Dealers
from contact in dealer.DealerContacts
select new { whatever you need from dealer or contact }
이는 where 절로 변환됩니다.
SELECT <columns>
FROM Dealer, DealerContact
WHERE Dealer.DealerID = DealerContact.DealerID
linq C#의 2개의 테이블을 내부 결합합니다.
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 }
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 data =(from t1 in dataContext.Table1 join
t2 in dataContext.Table2 on
t1.field equals t2.field
orderby t1.Id select t1).ToList();
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 };
학생 및 과정 테이블이 기본 키와 외부 키 관계를 갖는 경우
대신 이걸 시도해봐
var dealer = from d in Dealer
join dc in DealerContact on d.DealerID equals dc.DealerID
select d;
var Data= (from dealer in Dealer join dealercontact in DealerContact on dealer.ID equals dealercontact.DealerID
select new{
dealer.Id,
dealercontact.ContactName
}).ToList();
var data=(from t in db.your tableName(t1)
join s in db.yourothertablename(t2) on t1.fieldname equals t2.feldname
(where condtion)).tolist();
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();
원하는 테이블 이름을 쓰고 필드 결과를 가져오도록 선택을 초기화합니다.
DealerContrac의 d1에서 d1.dealearid는 d2.dealerid와 같다.dealerid를 새로 선택하십시오.*}
베스트의 예
: " " " " : "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}
언급URL : https://stackoverflow.com/questions/37324/what-is-the-syntax-for-an-inner-join-in-linq-to-sql
'programing' 카테고리의 다른 글
| WPF TextBlock과 TextBox 사이에 차이가 있습니까? (0) | 2023.04.10 |
|---|---|
| 열에 다른 열의 값이 포함되어 있는지 확인하시겠습니까? (0) | 2023.04.10 |
| Excel CSV - 번호 셀 형식 (0) | 2023.04.10 |
| git 기본 재배치 실행 취소 (0) | 2023.04.10 |
| JSON을 편집하기 위한 Emacs 모드 (0) | 2023.04.05 |