【问题标题】:Display Parent Values if Specified Child Values are NULL如果指定的子值为 NULL,则显示父值
【发布时间】:2014-11-20 03:37:13
【问题描述】:

我有一个 MVC 5 站点连接到一个 mysql 数据库并使用实体框架(db first)。

在我的 Products 表中,一个父项可以有许多子项。在这种情况下,我们将父项用作概览或容器,也就是父项是 PRODUCT-XYZ 概览,子项是它的变体(PRODUCT-XYZ-1-INCH、PRODUCT-XYZ-2-INCH,...)。由于会计来源的组织方式必须保持这种结构。

使用 linq,很容易在一个类别中列出产品,通过指定的 id 提取产品信息等。但是我不知道如何使用 linq 查询来完成以下操作。

=====================

场景 #1

给定子的 ProductID,拉出所有子列,如果这些列是空白的,则用父(派生自 ParentID)覆盖一些指定的列。

例如,给定 ID 为 2 的子代,Description 和DetailedDescription 为空,因此使用其父代(ID 为1)的值。

=====================

场景 #2

与场景 #1 相同,但在列表情况下。列出 CategoryID 1 中的所有产品时,如果它是父级,则只使用父级值,如果它是子级并且在 Description、DetailedDescription 列中有空值,则使用这些值。如果它是一个有自己价值观的孩子,那就使用那些。

表格:产品

------------------------------------------------------------------    
| ID | ParentID | ExternalID | Description | DetailedDescription |
------------------------------------------------------------------    
| 1  | NULL     | 3829       | Content     | Content             |
------------------------------------------------------------------    
| 2  | 1        | 4837       | NULL        | NULL                |
------------------------------------------------------------------    
| 3  | 1        | 9283       | Content     | Content             |
------------------------------------------------------------------    
| 4  | 1        | 6382       | NULL        | NULL                |
------------------------------------------------------------------    
| 5  | NULL     | 3234       | Content     | Content             |
------------------------------------------------------------------    
| 6  | 5        | 9283       | NULL        | NULL                |
------------------------------------------------------------------    
| 7  | 5        | 2983       | Content     | Content             |
------------------------------------------------------------------    

表格:类别

-------------------------------
| ID | ExternalID | Name      |
-------------------------------
| 1  | 2546       | Tools     | 
-------------------------------
| 2  | 3545       | Widgets   | 
-------------------------------

表格:Category_Product

--------------------------
| CategoryID | ProductID |
--------------------------
| 1          | 1         | 
--------------------------
| 1          | 2         | 
--------------------------
| 1          | 7         | 
--------------------------

=====================

附加说明

我一直在提取特定产品的详细信息,如下所示:

Product product = (from p in db.Products.Include("Assets").Include("RelatedProducts")
                               where p.ID == id
                               select p).SingleOrDefault();

我使用它的产品提取一个类别:

Category category = (from cats in db.Categories.Include("SubCategories").Include("Products")
                           where cats.ID == id
                           select cats).SingleOrDefault();

如果我没有提供足够的信息,请告诉我,即使是一般性的指导也会有所帮助。

【问题讨论】:

    标签: c# mysql linq asp.net-mvc-5


    【解决方案1】:
    public class ProductVM
    {
       public int Id { get; set; }
       public string Desc { get; set; }
       public string DetailDesc { get; set; }
       public List<CategoryVM> cats { get; set; }
    }
    public class CategoryVM
    {
        public int Id { get; set; }
        public int ParentId { get; set; }
        public string Desc { get; set; }
        public string DetailDesc { get; set; }
    }
    
    
     ProductVM product = new ProductVM();
    
      using (YourEntities context = new YourEntities ())
      {                 
          product = (from x in context.Parents
                     where x.Id == 1
                     select new ProductVM
                     {
                         Id = x.Id,
                         Desc = x.Descr,
                         DetailDesc = x.DetailDesc
                      }).FirstOrDefault();
    
        product.Categegories = (from x in context.Children
                               where x.ParentId == product.Id
                              select new CategoryVM
                              {
                                 Id = x.Id,
                                 ParentId = (int)x.ParentId,
                                 Desc = x.Descr == null ? product.Desc : x.Descr,
                DetailDesc = x.DetailDesc == null ? product.DetailDesc : x.DetailDesc
                                }).ToList();
    
               }
    

    如果您不熟悉?,它被称为三元运算符。它基本上是 if/else 的简写。在英语中,代码行显示为...“如果x.Descrnull,则使Desc 的值等于product.Desc 否则Desc 等于x.Desc

    您的ProductVM 将包含您产品的所有值并包含其类别列表,如果 x 字段为空,则该特定类别将从产品中获取值

    编辑:看起来我的产品 - 类别关系倒退了,但这让你知道该怎么做

    【讨论】:

    • 我绝对可以使用它,谢谢。翻转产品和类别并进行了一些测试,似乎效果很好。我陷入了一种思维框架,即一切都需要在一个查询中完成。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 2021-05-16
    相关资源
    最近更新 更多