![](/img/trans.png)
[英]How to retrieve data from table “a” based on different values in table “b”
[英]Is there any way to retrieve data based on multple where from multiple table
我一直在嘗試以各種可能的方式做到這一點。 我有表名employees等表名資格
每個員工都有資格,但在最初的基礎上,每個員工的資格並沒有被填入數據庫。
我也嘗試過 VIA EF 和 LINQ 以及原始的SqlConnection
和SqlCommand
,但仍然沒有得到好的結果。
擁有 PHD 的員工檢索方式為
ViewData["lecturer_phd"] = _context.TblQualification
.Count(q => q.QualificationType == 4 &&
q.Employee.DesignationCode == 3);
和非博士應該回來
ViewData["lecturer_nphd"] = _context.TblEmployees
.Count(e => e.DesignationCode == 3 &&
e.EmployeeQualifications.Any(q => q.QualificationType != 4));
但這不起作用,我也不熟悉 LINQ 但我也試過了,沒有任何結果。
原始 SQL 查詢使用以下代碼:
SqlConnection con = new SqlConnection(_context.Database.GetConnectionString());
DbCommand cmd = new SqlCommand("SELECT Count(*) FROM [DbBzuCC].[dbo].[tblEmployees] LEFT JOIN tblQualifications ON tblEmployees.Employee_Code = tblQualifications.Employee_Code AND tblQualifications.qualification_type != 4 WHERE tblEmployees.Designation_Code = 3",
con);
con.Open();
ViewData["lecturer_nphd"] = (int) cmd.ExecuteScalar();
con.Close();
但一切都是徒勞的。 任何幫助將不勝感激。 我將感謝社區的任何幫助。 提前致謝
實際上,qualification_type 是 null 沒有為任何員工輸入,所以在 sql 查詢中我們需要檢查
ISNULL 像這樣
SELECT COUNT(*)
FROM [DbBzuCC].[dbo].[tblEmployees]
Left join tblQualifications ON tblEmployees.Employee_Code = tblQualifications.Employee_Code
WHERE tblEmployees.Designation_Code = 3 AND ISNULL(tblQualifications.qualification_type,1) != 4
在實體框架中你可以這樣做
_context.TblEmployees
.Count(e => e.DesignationCode == 3 && e.EmployeeQualifications.All(q => q.QualificationType != 4));
感謝大家寶貴的時間。
問候,
所以你有Employees
和Qualifications
,並且是一對多的關系:每個Employee
有零個或多個Qualifications
,每個Qualification
都是恰好一個Employee
的Qualification
,即外鍵 EmployeeCode 所指的 Employee 。
如果您遵循實體框架約定,您將擁有類似於以下內容的類:
class Employee
{
public int Id {get; set;}
public string Name {get; set;}
...
// Every Employee has zero or more Qualifications (one-to-many)
public virtual ICollection<Qualification> {get; set;}
}
class Qualification
{
public int Id {get; set;}
public string Description {get; set;}
...
// Every qualification is the qualification of exactly one Employee using foreign key
public int EmployeeId {get; set;}
public virtual Employee Employee {get; set;}
}
這足以讓實體框架使用主鍵和外鍵檢測表、表中的列以及表之間的關系。
在實體框架中,表的列由非虛擬屬性表示。 虛擬屬性表示表之間的關系(一對多、多對多)
外鍵Qualification.EmployeeId
是一個真實的列,因此它是非虛擬的。 屬性Qualfication.Employee
表示關系,因此它是虛擬的。
如果您偏離約定,就像您在外鍵中所做的那樣,只需要屬性也不需要流利的 API。 在 DbContext.OnModelCreating 中:
modelBuilder.Entity<Employee>().HasKey(employee => employee.EmployeeCode);
modelBuilder.Entity<Employee>().HasMany(employee => employee.Qualifications)
.WithRequired(qualification => qualification.Employee)
.HasForeignKey(qualification => qualification.EmployeeCode);
換句話說:每個 Employee 在 EmployeeCode 屬性中都有一個主鍵。 每個 Employee 在 Employee.Qualifications 屬性中都有零個或多個 Qualifications。 每個 Qualification 在屬性 Qualification.Employee 中只有一個 Employee,使用外鍵 Qualificatioin.EmployeeCode。
要求:為具有 Designation_Code == 3 的員工計算具有qualification_type != 4 的資格數量
使用虛擬 ICollection 這很簡單:
int qualificationTypeToIgnore == 4;
int employeeDesignationCode == 3;
var result = dbContext.Qualifications.Where(qualification =>
qualification.QualificationType != qualificationTypeToIgnore &&
qualification.Employee.DesignationCode == employeeDesignationCode)
.Count();
換句話說:從所有 Qualifications 中,只保留那些 QualificationType 不等於qualificationTypeToIgnore 的 Qualifications,它也屬於 DesingationCode 等於 employeeDesignationCode 的 Employee。 計算剩余的資格數。
實體框架知道一對多關系,並且會為你做屬性 Join。
有些人不喜歡使用虛擬屬性,他們更喜歡自己進行連接。
var eligibleEmployees = dbContext.Employees
.Where(employee => Employee.DesignationCode == employeeDesignationCode);
var eligibleQualifications = dbContext.Qualifications
.Where(qualification => qualification.QualificationType != qualificationTypeToIgnore);
var result = eligibleQualifications.Join(eligibleEmployees,
qualification => qualification.EmployeeCode, // from every Qualification take the foreign key
employee => employee.EmployeeCode, // from every Employee take the primary key
// parameter resultSelector: from every (qualification, employee) combination make one new
(qualification, employee) => new {qualification, employee})
.Count();
由於您不會使用連接結果,您可以簡化參數 resultSelector:
(qualification, employee) => 1
或者讓實體框架做連接:
var result = eligibleQualifications.Where(qualification => eligibleEmployees
.Any(employee => employee.EmployeeCode == qualification.EmployeeCode))
.Count();
從所有符合條件的資格中,計算具有 EmployeeCode 的資格的數量,該 EmployeeCode 也是符合條件的員工中的 EmployeeCode。
恕我直言,使用虛擬屬性的解決方案是最優雅的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.