我有一个方法是返回一个列表>>,但我遇到了一个错误:
无法将源类型System.Collections.Generic.List<{Key:int,Value:string}>转换为目标类型System.Collections.Generic.KeyValuePair<'int,string'> 我正在尝试将Linq选择语句变成一个新的列表,但我不明白初始化列表是否有问题,或者是如何在Linq语句中获取值的问题。
以下是该方法:
以下是填充列表的方法:
无法将源类型System.Collections.Generic.List<{Key:int,Value:string}>转换为目标类型System.Collections.Generic.KeyValuePair<'int,string'> 我正在尝试将Linq选择语句变成一个新的列表,但我不明白初始化列表是否有问题,或者是如何在Linq语句中获取值的问题。
以下是该方法:
public static List<KeyValuePair<int,string>> GetStatus(int status)
{
List<KeyValuePair<int,string>> st = SelectList.GetStatuses();
List<KeyValuePair<int,string>> tp;
switch(status)
{
case (int)Status.Completed:
tp = st.Where(s => s.Key == (int)Status.Completed ||
s.Key == (int)Status.NotValid)
.Select(s => new { s.Key, s.Value }).ToList();
case (int)Status.Open:
tp = st.Where(s => s.Key == (int)Status.Open ||
s.Key == (int)Status.Reviewed)
.Select(s => new { s.Key, s.Value }).ToList();
default:
break;
}
return tp;
}
以下是填充列表的方法:
public static List<KeyValuePair<int, string>> GetStatuses()
{
using (var con = new SqlConnection())
{
var sql = @"SELECT ID [Key], Text [Value] from Statuses";
return (con.Query<KeyValuePair<int, string>>(sql.ToString(), commandType: commandType:Text) ?? Enumerable.Empty<KeyValuePair<int, string>>()).ToList();
}
}