简体   繁体   English

实体框架内部联接

[英]Inner Join on Entity Framework

There is a table name Product : 有一个表名称Product

|ProductID|ProductName|
  1            abc
  2            xyz

and there is another table Product Status . 并且还有另一个表Product Status

The column Result in the below has three values: 下面的“ Result ”列具有三个值:

Result
Value | Meta
 0        Not checked
 1        Failed
 2        Passed  

ProductCheckList
ID(AutoGenerated) | ProductID(Foreign Key) | Stage | Result |
    1                  1                         1      1
    2                  1                         2      2 

In this table every product has to go through five different stages. 在此表中,每个产品必须经历五个不同的阶段。 If the product passes all the stages then the product is given quality checked status , If the product fails any on of the stages is is given as quality failed and send back to production. 如果产品通过所有阶段,则产品处于质量检查状态;如果产品不合格,则给出任何阶段的质量不合格,然后送回生产。

I have to show a list of all the products with there quality status and depending on its quality state highlight the row with different row color. 我必须显示所有具有质量状态的产品的列表,并根据其质量状态突出显示具有不同行颜色的行。 We are using Entity Framework and I am new to this. 我们正在使用实体框架,对此我是陌生的。

I have thought of making a wrapper class for this. 我曾考虑为此做一个包装器类。

Public class ProductWrapper
{
    public Product product{get;set;}
    Public string QualityStatus{get;set;}
    public  string BgColor {get;set;}
}

I am writing this LINQ query: 我正在编写此LINQ查询:

UtilitiesEntities context = new UtilitiesEntities();
            List<ProductWrapper> wrapperList = new List<ProductWrapper>();
            var request = from product in context.Product
                          join productCheck in context.ProductCheckList
                          on product.productId equals productCheck .productID
                           // may be do group by or something i get the result and assign the values.
                          select new List<ProductWrapper>
                          {

                          };

I am not able to write the query and add the where condition to fetch the result a list of wrapper class to pass to my view with the desired result. 我无法编写查询并添加where条件来获取结果的包装类列表,以将所需的结果传递给我的视图。

If I understood correctly your request, you want something like this: 如果我正确理解了您的请求,则需要以下内容:

string goodQualityColor = "Green";
string badQualityColor = "Red";
string notCheckedColor = "Gray";
string notCheckedStatus = "Not Checked";
string failedStatus = "Failed";
string passedStatus = "Passed";

Dictionary<int,string> results= new Dictionary<int,string>();
results.Add(2,goodQualityColor);
results.Add(1,badQualityColor);
results.Add(0,notCheckedColor);

Dictionary<int,string> qualityStatuses = new Dictionary<int,string>();
results.Add(2,passedStatus);
results.Add(1,failedStatus);
results.Add(0,notCheckedStatus);

var request = (from product in context.Product
              join productCheck in context.ProductCheckList
              on product.productId equals productCheck.productID
              select new
              {
                 Product = product,
                 QualityStatus = productCheck.Result,
                 Result = productCheck.Result
              }).ToList();

var finalResults = (from req in request
                    select new ProductWrapper
                    {
                      Product = req.Product,
                      QualityStatus = qualityStatuses.FirstOrDefault(s => s.Key == req.QualityStatus).Value,
                      BgColor = results.FirstOrDefault (s => s.Key == req.Result).Value  
                    }).ToList<ProductWrapper>(); 

I would create an outer join using the into keyword and then do my statusCheck in the select part. 我将使用into关键字创建一个外部联接,然后在select部分中执行statusCheck。
Could would look something like this : 可能看起来像这样:

var data = (from product in context.Product
            join productCheck in context.ProductCheckList on product.productId equals productCheck.productID into productChecks
            select new ProductWrapper
            {
                product = product,
                passedBool = productChecks.All(pc => pc.Result == 2) && productChecks.Count() == 5
            }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM