i have an issue when getting different record from related table : for example i have two table name as Tag and Product.I'm going to set tag for each product.Product table item what exits on tag table not show if this item exist on Product table.
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;
}
}
}
So what are you trying to do? When you do
i.Title
what are you expecting to happen?If you're trying to find the tags that aren't in the pTagList then, as you've seen, your two foreach loops are flawed and won't do that.
Some linq will help though ...