我的 sql server 数据库中有一个名为 product 和 category 的表。 Product 表包含用于存储映射类别的列 category_id。一个产品可以映射到任意数量的类别,例如。category_id(1,25,44,11,21)
,映射 ID 将存储为逗号分隔的字符串。
现在我想使用 linq 从产品表中检索产品,其中产品包含从下拉菜单中选择的类别。
例如,我想检索产品 where category id =1
。我的 linq 查询是
var prodlst = (from p in db.Products
where (p.PCategory_Id.EndsWith(""+categoryid+"")||
p.PCategory_Id.Contains("," + categoryid + ",") ||
p.PCategory_Id.Contains("" + categoryid + ",") ||
p.PCategory_Id.Contains("," + categoryid + ""))
select new ProductBO
{
Id = p.Id,
Product_Name = p.Name,
Price = p.Price
}).ToList();
它检索类别 id = 1 或 11 或 21 的所有值,但我只想检索类别 id 恰好为 1 的值
最佳答案
我不确定 EntityFramework 是否可以翻译这个。如果它不尝试使其可枚举。
db.Products.Where(t=> PCategory_Id.Split(',').Select(int.Parse).Contains(categoryid));
db.Products.AsEnumerable().Where(t=> PCategory_Id.Split(',').Select(int.Parse).Contains(categoryid));
关于c# - 如何使用 C# 中的 linq 检查存储在具有给定值的数据库列中的逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43775306/