我正在編寫LINQ to SQL語句,並且我遵循C#中ON
子句的正常內連接的標準語法。LINQ to SQL中的內部連接的語法是什麼?
你怎麼代表以下的LINQ到SQL:
select DealerContact.*
from Dealer
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
我正在編寫LINQ to SQL語句,並且我遵循C#中ON
子句的正常內連接的標準語法。LINQ to SQL中的內部連接的語法是什麼?
你怎麼代表以下的LINQ到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;
既然你正在尋找的接觸,而不是經銷商。
謝謝,從現在開始,我將使用**明智的名稱**作爲最佳實踐,這在linq中是有意義的,而不是從`c或從t1`開始的 – stom 2016-01-14 07:26:42
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();
嗨, 請問你能告訴我這部分是關於什麼? 狀態=(ContactStatus)c.StatusID 我在片段感興趣expecially:(ContactStatus)c.StatusID 問候 馬裏烏什 – Mariusz 2010-03-12 23:55:11
@aristo - 看代碼,我猜'ContactStatus`是真的是一個枚舉,`c.StatusID`不是一個真正的ID,而是枚舉的數值。如果我是對的,`(ContactStatus)c.StatusID`實際上只是將一個整數轉換爲一個枚舉。 – 2010-07-14 22:13:43
而且因爲我更喜歡錶達鏈語法,這裏是你如何與做:
var dealerContracts = DealerContact.Join(Dealer,
contact => contact.DealerId,
dealer => dealer.DealerId,
(contact, dealer) => contact);
如果您需要過濾或選擇來自*兩個*聯合表格的字段,而不是僅在兩個表格之一的字段(此答案的示例中的DealerContact表格)中,則可以使用以下示例:http://stackoverflow.com/ a/29310640/12484 – 2015-03-27 21:41:44
基本上LINQ 加入運營商提供對SQL沒有好處。即下面的查詢
var r = from dealer in db.Dealers
from contact in db.DealerContact
where dealer.DealerID == contact.DealerID
select dealerContact;
將導致INNER JOIN在SQL
加入,因爲它是更有效是IEnumerable的<>有用:
from contact in db.DealerContact
條款將被用於重新執行每經銷商 但對於IQueryable <>情況並非如此。另外加入不那麼靈活。
您創建了一個外鍵,LINQ-to-SQL爲您創建導航屬性。然後每個Dealer
將有一個DealerContacts
的集合,您可以選擇,過濾和操作。
from contact in dealer.DealerContacts select contact
或
context.Dealers.Select(d => d.DealerContacts)
如果你不使用導航屬性,你就錯過了對LINQ到SQL的主要優點之一 - 對象圖映射的一部分。
哦,男人,你節省了我的時間,我不需要再處理這些愚蠢的連接了! – Tomas 2013-01-31 15:16:30
其實,往往最好不要加入,在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
帶有多個「from」子句的LINQ查詢(如本例中)在表達式鏈式語法中看起來如何?可能嗎? – 2015-05-27 20:07:11
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 };
如果學生和課程表通過主鍵和外關鍵關係
一個最佳示例
表名稱: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}
使用LINQ joins進行內連接。
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 dealer = from d in Dealer
join dc in DealerContact on d.DealerID equals dc.DealerID
select d;
試試這個:
var data =(from t1 in dataContext.Table1 join
t2 in dataContext.Table2 on
t1.field equals t2.field
orderby t1.Id select t1).ToList();
要由聰明人延長表達鏈語法answer:
如果你想要做的事(像過濾器或選擇)來自兩個表格的字段被連接在一起 - 而不是僅僅其中之一兩個表 - 你可以在最後一個參數的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 }
...我們構建具有作爲屬性的代理商聯繫和經銷商的記錄,他們所有的字段沿着新的匿名類型的對象。
然後,我們可以使用這些記錄中的字段來篩選和選擇結果,如示例的其餘部分所示,該示例使用dc_d
作爲我們構建的匿名對象的名稱,該對象同時具有DealerContact和Dealer記錄其屬性。
內加入Linq中的C#兩個表
VAR的結果=
從Q1在表1
表2中加入Q2
上q1.Customer_Id等於q2.Customer_Id
選擇新{q1.Name,Q1。移動,q2.Purchase,q2.Dates}
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();
寫出您想要的表名,並初始化select以獲取字段的結果。
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();
您可能會發現[LINQ中的SQL查詢](http://www.codeducky.org/sql-queries-in-linq/#inner-join)有幫助。它解釋瞭如何將常見的SQL查詢(包括內部連接)轉換爲LINQ語法。 – 2014-05-13 22:18:51
如果你在表格之間有一個外鍵,你應該看看下面的Kirk Broadhurst的答案。 – Guilherme 2016-11-20 22:35:21
你應該複用你的表名。關於許多經銷商的表格(有關條目)應稱爲*經銷商*,而不是經銷商。 – ANeves 2017-08-21 10:32:25