我有兩張桌子:
Table1
Id ArticleName ArticleTypeId
1 Blah Blah 3
2 Helo Blah 5
和
Table2
ArticleTypeId TypeName
3 Business
5 Construction
我正在嘗試加入 TableA 和 TableBArticleTypeId并基本上回傳 Table1 中的所有內容和 Table2 中的 TypeName
這是我正在嘗試做的事情,但我不確定在陳述句中編輯 SELECT 以包含 TypeName
var articles = (from s in _context.Articles
join b in _context.ArticleTypes on s.ArticleTypeId equals b.ArticleTypeId
select s).ToList();
或者有沒有更簡單的方法來做到這一點?
目標:
Id ArticleName TypeName
1 Blah Blah Business
2 Helo Blah Construction
uj5u.com熱心網友回復:
因此,您有兩個表,一個帶有Articles(Table1) 的表和一個帶有ArticleTypes(Table2) 的表。我決定給你的表命名是有意義的,這使得討論更容易。
Articles和之間是一對多的關系ArticleTypes:每一個Article都有一個ArticleType,即外鍵ArticleTypeId參考的Article型別。每個 ArticleType 都有零個或多個參考它的文章。
您正在使用物體框架。如果您遵循Entity Framework Coding Conventions,您將擁有類似于以下內容的類。
class Article
{
public int Id {get; set;}
public string Name {get; set;}
// every Article has one ArticleType, namely the one that the foreign key refers to
public int ArticleTypeId {get; set;}
public virtual ArticleType ArticleType {get; set;}
}
class ArticleType
{
public int Id {get; set;}
public string TypeName {get; set;}
// every ArticleType has zero or more Articles referring to it (one-to-many)
public virtual ICollection<Article> Articles {get; set;}
}
在物體框架中,非虛擬屬性是指表的列;虛擬屬性是指表之間的關系(一對多,多對多,...)
外鍵 ArticleTypeId 是一個實列,因此該屬性是非虛的。屬性 ArticleType 是虛擬的,因為它表示一對多關系。
為了完整性,您的 DbContext:
class MyWarehouse : DbContext
{
public DbSet<Article> Articles {get; set;}
public DbSet<ArticleType> ArticleTypes {get; set;}
}
我正在嘗試在 ArticleTypeId 上加入 TableA 和 TableB 并基本上從 Table1 中回傳所有內容,從 Table2 中回傳 TypeName
在你定義了你的類之后,你的查詢就很容易了。最簡單的方法是使用虛擬屬性。
使用虛擬屬性
要求給我所有文章的 ID 和名稱,每篇文章都有它的 TypeName。
using (var wareHouse = new MyWareHouse(...))
{
var requestedArticles = wareHouse.Articles.Select(article => new
{
// Select only the Article Properties that you plan to use
Id = article.Id,
Name = article.Name,
TypeName = article.ArticleType.TypeName,
});
// Process the requested Articles before disposing the wareHouse
}
換句話說:從 Articles 表中的每篇文章中獲取 Id、Name 以及它擁有的唯一一個 TypeName。
物體框架知道 Articles 和 ArticleTypes 之間的關系。因為您使用虛擬屬性Article.ArticleType,所以它知道要執行哪個連接。
使用虛擬屬性,您還可以獲得每個 ArticleType 以及具有此 ArticleTypes 的所有文章
var constructionArticles = wareHouse.ArticleTypes
.Where(articleType => articleType.TypeName == "construction")
.Select(articleType => new
{
Id = articleType.Id,
TypeName = articleType.TypeName,
// fetch all articles that have this TypeName
Articles = articleType.Articles.Select(article => new
{
Id = article.Id,
Name = article.Name,
// no need to fetch the foreign key, you already got this value
// ArticleTypeId = article.ArticleTypeId,
})
.ToList(),
})
.ToList();
物體框架知道這種關系,并會為你做正確的(組)加入。
您是否注意到使用虛擬屬性的感覺是多么自然?
自己加入
有些人不想使用虛擬屬性,他們更喜歡自己做(Group-)joins。
使用具有引數 resultSelector 的方法 Join 的多載,因此您可以指定所需的結果。
// Join Articles with ArticleTypes
var requestedArticles = wareHouse.Articles.Join(wareHouse.ArticleTypes,
// from every Article take the foreign key
article => articleTypeId,
// from every ArticleType take the primary key
articleType => articleType.Id,
// parameter resultSelector:
// take each article and its one and only matching ArticleType to make one new
(article, articleType) => new
{
Id = article.Id,
Name = article.Name
TypeName = articleType.TypeName,
});
如果你有一對多的關系,比如學校和他們的學生,客戶和他們的訂單,或者 ArticleTypes 和他們的文章,使用 GroupJoin 并從“一”端開始。如果你想要學生,他就讀的學校的每個學生,使用加入,并從“多”端開始。
var schoolsWithTheirStudents = dbContext.Schools
.Where(school => school.City == ...) // if you don't want all Schools
.GroupJoin(dbContext.Students,
// from every School take the primary key
school => school.Id,
// from every Student take the foreign key to the School he attends
student => student.SchoolId,
// resultSelector: take each Schools with its matching Students to make one ned
(school, studentsWhoAttendThisSchool) => new
{
// Select only the School properties that you plan to use:
Id = school.Id,
Name = school.Name,
Address = school.Address,
...
// studentsWhoAttendThisSchool is a queryable sequence,
// so you can use LINQ on it:
Students = studentsWhoAttendThisSchool.Select(student => new
{
Id = student.Id,
Name = student.Name,
...
})
.ToList(),
});
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510628.html
標籤:C#林克实体框架核心
