2014-09-25 59 views
0

我有一個MVC 5網站連接到一個MySQL數據庫和使用實體框架(數據庫第一)。顯示父值如果指定子值爲NULL

在我的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(); 

請讓我知道,如果我的天堂」 t提供了足夠的信息,即使通用的指導也會有所幫助。

回答

0
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字段爲空,該特定類別將採取從價值產品

編輯:它看起來像我的產品 - 類別關係倒退,但是這給你做什麼

+0

我可以肯定地與工作JIST,謝謝。翻轉產品和類別並運行一些測試,似乎運行良好。我陷入了一個想法,即一切都需要在一個查詢中完成。非常感激! – 2014-09-26 16:17:34