C# linq中查询的位置

C# linq中查询的位置,c#,linq,C#,Linq,这是我将值绑定到上述模型的方法,它包含内联SQL查询 public Class SomeModel { public int Id { get; set;} public string Name {get; set;} } 但是这并不是选择任何值总是零过滤结果,我如何才能正确地写下它呢?使用数组/集合而不是列表,并检查id是否在其中 IEnumarable<SomeModel> filterValues = GetData.Where(Id=> Id.ToStr

这是我将值绑定到上述模型的方法,它包含内联SQL查询

public Class SomeModel
{
   public int Id { get; set;}

   public string Name {get; set;}
}

但是这并不是选择任何值总是零过滤结果,我如何才能正确地写下它呢?

使用数组/集合而不是列表,并检查id是否在其中

IEnumarable<SomeModel> filterValues = GetData.Where(Id=> Id.ToString().Contains(filterIds));
IEnumarable filterValues=ActualGetData()。其中(n=>filterIds.Contains(n.Id))
其中ActualGetData()返回IEnumarable。

在当前代码中,GetData()返回Drew Kennedy提到的ActionResult

如果您确定“Id”集合的所有元素都可以转换为数字,则可以使用以下方法

IEnumarable<SomeModel> filterValues = ActualGetData().Where(n => filterIds.Contains(n.Id))
var models=newlist()//请考虑此集合来自某些服务或存储库。
var id=filterIds.Split(',')。选择(x=>int.Parse(x));
IEnumarable filterValues=models.Where(n=>Ids.Contains(n.Id));

你的代码无法编译,所以我在这里发布的只是逻辑代码。您可以根据自己的要求使用它。

这可能是一种方法:

var models = new List<SomeModel>(); //Consider that this collection is coming from some service or repository.
var Ids = filterIds.Split(',').Select(x => int.Parse(x));
IEnumarable<SomeModel> filterValues = models.Where(n => Ids.Contains(n.Id));

你用的是LINQ查询吗?因为它甚至不会编译。GetData返回ActionResult。你怎么能在上面运行Where方法呢?有两件事不对,这更多的是和问题有关。1) GetData是一个函数,因此需要括号。2) 它返回ActionResult,因此无论如何都不会编译。实际上,由于出现异常,字符串到整数的转换是错误的,无论如何,感谢您的支持!
var models = new List<SomeModel>(); //Consider that this collection is coming from some service or repository.
var Ids = filterIds.Split(',').Select(x => int.Parse(x));
IEnumarable<SomeModel> filterValues = models.Where(n => Ids.Contains(n.Id));
string filterIds = "12,13,14,15";

//Convert filterIds string into a integers collection
var ids=filterIds.Split(',').Select(int.Parse);

IEnumarable<SomeModel> filterValues = GetData().Where(sm=> ids.Contains(sm.Id));
public IEnumarable<SomeModel> GetData()
{
  IEnumarable<SomeModel> alltheListValues = ....
  //...
  return alltheListValues;
}