我有代码从数据库中获取id列表。代码使用LINQ下面的
完成var resutls = (from v in con.NewsLatter
where v.SendStatus == "Active"
select new { v.Id }).ToList;
它从名为NewsLater的表中返回数据库中的所有id。现在我要做的就是填写
中的所有内容List<Int32> IdList = new List<Int32>{ //list of id want to fill here in List<Int32> };
var min = IdList .Where(x=>x>12).Min();
return min;
当我尝试填写如下所示
时List<Int32> IdList = new List<Int32> { Convert.ToInt32(resutls) };
var min = IdList .Where(x => x > 12).Min();
return min;
我有一个例外:
Unable to cast object of type
'System.Collections.Generic.List`1[<>f__AnonymousType0`1[System.Int32]]' to type
'System.IConvertible'.
任何人都知道这里有什么问题吗?
答案 0 :(得分:0)
尝试使用AddRange method:
List<Int32> IdList = new List<Int32>()
IdList.AddRange(resutls)
假设resutls
本身就是List<Int32>
,看起来就像是来自你的代码。
编辑:
将您的查询更改为:
var resutls = (from v in con.NewsLatter
where v.SendStatus == "Active"
select v.Id).ToList();
您只需选择v.Id
,即无需创建新对象new { v.Id }
。
答案 1 :(得分:0)
尝试一下,首先在int变量中声明转换后的值,然后给出值。
<强>被修改强>
foreach (var result in resutls)
{
int convertedResult = Convert.ToInt32(result);
List<Int32> IdList = new List<Int32> { convertedResult };
IdList.Add(convertedResult);
}