Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中使用linq检查存储在具有给定值的数据库列中的逗号分隔值#_C#_Sql Server_Asp.net Mvc_Entity Framework_Linq - Fatal编程技术网

C# 如何在c中使用linq检查存储在具有给定值的数据库列中的逗号分隔值#

C# 如何在c中使用linq检查存储在具有给定值的数据库列中的逗号分隔值#,c#,sql-server,asp.net-mvc,entity-framework,linq,C#,Sql Server,Asp.net Mvc,Entity Framework,Linq,我的sql server数据库中有一个名为product和category的表。Product表包含用于存储映射类别的列category_id。一个产品可以映射到任意数量的类别,例如类别id(1,25,44,11,21),映射id将存储为逗号分隔的字符串 现在我想使用linq从产品表中检索产品,其中产品包含下拉菜单中选择的类别 例如,我想检索产品,其中category id=1。我的linq查询是 var prodlst = (from p in db.Products w

我的sql server数据库中有一个名为product和category的表。Product表包含用于存储映射类别的列category_id。一个产品可以映射到任意数量的类别,例如
类别id(1,25,44,11,21)
,映射id将存储为逗号分隔的字符串

现在我想使用linq从产品表中检索产品,其中产品包含下拉菜单中选择的类别

例如,我想检索产品
,其中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();

它检索category id=1、11或21的所有值,但我只想在category 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));

这是在你的语法方式。只有第2个子句需要是“Contains”(我也将其更改为==):


这是非常危险的代码,在linq中使用
contains
语法意味着
类似于
,这就是为什么您的结果将显示任何类别都包含1。首先考虑拆分类别ID,然后使用<代码>相等< /代码>运算符来获得您想要的确切类别。您是否拥有数据库设计的控制权?我认为更好的设计是有3个表:Products、Categories、ProductCategories(映射表),而不是存储逗号分隔的CategoryId。上面的代码抛出了这个运行时错误“无法创建'System.Object'类型的常量值。在这个上下文中只支持基元类型或枚举类型。”“使用==而不是等于“”,其中p.pcCategory_Id==”“+categoryid”
var prodlst = (from p in db.Products
       where ( (p.PCategory_Id == categoryid) ||
       p.PCategory_Id.Contains("," + categoryid + ",") ||
       p.PCategory_Id.StartsWith(categoryid + ",") ||
       p.PCategory_Id.EndsWith("," + categoryid))
       select new ProductBO
       {
         Id = p.Id,
         Product_Name = p.Name,
         Price = p.Price
}).ToList();
var prodlst = (from p in db.Products
                               where (p.PCategory_Id == ""+categoryid ||
                               p.PCategory_Id.Contains("," + categoryid + ",") ||
                               p.PCategory_Id.StartsWith(categoryid + ",") ||
                               p.PCategory_Id.EndsWith("," + categoryid))
                               select new ProductBO
                               {
                                   Id = p.Id,
                                   Product_Name = p.Name,
                                   Price = p.Price
                               }).ToList();