2013-12-19 83 views
0

從相關表中獲取不同記錄時,我遇到問題:例如,我將兩個表名稱作爲標記和產品。我要爲每個產品設置標記。產品表項如果此商品存在於商品表格中,則標籤表格上的退貨不會顯示。從相關表中獲取不同記錄時出現問題

public class Tags 
{ 
    public int TagId {get;set;} 
    public string Title {get;set;} 
} 

public class Products 
{ 
    public int Id {get;set;} 
    public string Title {get;set;} 
} 

public class ProductsTag 
{ 
    public int Id {get;set;} 
    public int ProductId {get;set;} 
    public int TagId {get;set;} 
} 

var tagList = List<Tags>(); 
tagList.Add(new Tags{TagId = 1, "Cacao"}); 
tagList.Add(new Tags{TagId = 2, "Banana"}); 
tagList.Add(new Tags{TagId = 3, "Chevy"}); 
tagList.Add(new Tags{TagId = 4, "Nuts"}); 

var productList = List<Products>(); 
productList.Add(new Products{Id=1, "Chocolate"}); 
productList.Add(new Products{Id=2, "Chocolate"}); 

var pTagList = List<ProductsTag>(); 
pTagList.Add(new ProductsTag{Id=1, ProductId=1, TagId=1}); 
pTagList.Add(new ProductsTag{Id=2, ProductId=1, TagId=4}); 
pTagList.Add(new ProductsTag{Id=3, ProductId=2, TagId=1}); 


foreach(var i in tagList) 
{ 
    foreach(var n in pTagList) 
    { 
     if(i.TagId!=n.TagId) 
     { 
      i.Title; 
     } 
    } 
} 
+0

它到底是什麼你;再詢問,在這裏?我沒有看到任何SQL,以及您提到的「問題」是什麼? – TJennings

回答

0

那麼,你想幹什麼?當你做i.Title你期望發生什麼?

如果您正在嘗試查找不在pTagList中的標記,那麼如您所見,您的兩個foreach循環存在缺陷,並且不會這樣做。

一些LINQ將有助於雖然...

var unusedTags = tagList 
     .Except(from tag in tagList 
      join productsTag in pTagList on tag.TagId equals productsTag.TagId 
      select tag); 
0

我希望這是你想要的

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<Tags> tagList = new List<Tags>(); 
      tagList.Add(new Tags { TagId = 1, Title = "Cacao" }); 
      tagList.Add(new Tags { TagId = 2, Title = "Banana" }); 
      tagList.Add(new Tags { TagId = 3, Title = "Chevy" }); 
      tagList.Add(new Tags { TagId = 4, Title = "Nuts" }); 

      List<Products> productList = new List<Products>(); 
      productList.Add(new Products { Id = 1,Title= "Chocolate" }); 
      productList.Add(new Products { Id = 2,Title= "Chocolate" }); 

      List<ProductsTag> pTagList = new List<ProductsTag>(); 
      pTagList.Add(new ProductsTag { Id = 1, ProductId = 1, TagId = 1 }); 
      pTagList.Add(new ProductsTag { Id = 2, ProductId = 1, TagId = 4 }); 
      pTagList.Add(new ProductsTag { Id = 3, ProductId = 2, TagId = 1 }); 

      List<string> missingstuff = new List<string>(); 

      foreach (var i in tagList) 
      { 
       int index = pTagList.FindIndex(item => item.TagId == i.TagId); 
       if (index < 0) 
       { 
        missingstuff.Add(i.Title);     
       }        
      } 
     } 

     public class Tags 
{ 
    public int TagId {get;set;} 
    public string Title {get;set;} 
} 

public class Products 
{ 
    public int Id {get;set;} 
    public string Title {get;set;} 
} 

public class ProductsTag 
{ 
    public int Id {get;set;} 
    public int ProductId {get;set;} 
    public int TagId {get;set;} 
} 


    } 

} 
相關問題