关于LINQ的子查询
C# code
var query = (from entity in ctx.Base_Commodity_Manufacturer where entity.IsDelete == false orderby entity.CreateTime descending select entity);
这是我的查询语句。
我从页面会取出id,存入字符串变量str中,结果就是这样:
string str = "1,5,11,15"; //会得到随机的编号
我想在query中直接使用str进行子查询,
就是sql语句一下
where id in str
在linq中应该如何去写呢?
------解决方案--------------------string str = "1,5,11,15";
var query = from entity in ctx.Base_Commodity_Manufacturer
where entity.IsDelete == false && str.Split(',').Contains(entity.id)
orderby entity.CreateTime descending
select entity;
------解决方案--------------------掉了一个括号,改进如下:
C# code
string str="1,5,11,15";
var query = (from entity in ctx.Base_Commodity_Manufacturer
where entity.IsDelete == false && str.Split(',').Any(c => c == entity.id)
orderby entity.CreateTime descending
select entity);