【发布时间】:2010-03-18 14:44:48
【问题描述】:
我不能在匿名类型的 linq 表达式中使用字典中的 TryGetValue()。
Dictionary<string, string> dict = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
public string getValueByKey(string value)
{
string sColumnType = "";
dict.TryGetValue(value, out sColumnType);
return sColumnType;
}
[WebMethod]
public string getData(string name)
{
var result = (from z in myObject
where z.name == name
select new
{
a = z.A,
b = z.B,
c=getValueByKey(z.C) //fails there
}).ToList();
return new JavaScriptSerializer().Serialize(result);
}
请告诉我如何通过字典中的键获取值?
【问题讨论】:
-
你得到什么错误信息?
-
@Luiscencio:在 Visual Studio 中,它没有红色下划线作为语法错误。但在调试中我发现了这样的事情:(“消息”:“表达式 LINQ to Entities 无法识别方法 \” System.String getValueByKey (System.String) \ “,因此它无法转换为存储库。", "StackTrace": "在 System.Data.Objects 中。
-
刚刚问了一些与dis相关的问题:stackoverflow.com/questions/2466495/…
-
所以..你需要实现一些讨厌的东西