【问题标题】:An issue when getting different record from related table从相关表中获取不同记录时的问题
【发布时间】:2014-01-07 19:06:21
【问题描述】:

我在从相关表中获取不同记录时遇到问题:例如,我有两个表名作为标签和产品。我将为每个产品设置标签。标签表上退出的产品表项不显示如果这个项目存在于 Product 表中。

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;
        }
    }
}

【问题讨论】:

  • 你到底想要什么,在这里?我没有看到任何 SQL,您提到的“问题”是什么?

标签: c# sql list set-difference


【解决方案1】:

那么你想做什么?当您执行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);

【讨论】:

    【解决方案2】:

    我希望这是你想要的

     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;}
    }
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2017-04-22
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多