积极答复者
static 方法里面为什么不能用select?

问题
-
你好,
我代码中:
/// <summary>
/// 得到某个用户需要展示的chart
/// </summary>
/// <returns></returns>
public static List<ChartDetail> GetUserChartDetail(string username)
{
if (ShowChartClauses.ContainsKey(username))
{ //下面这段没有效果,我想根据参数做筛选,用linq,ShowChartDetail是List<Chartdetail>类型,在静态方法里不能用吗?//from chartdetail in ShowChartDetail where pname = username
}
return null;
}谢谢!
答案
-
linq的使用跟Static 方法没有关系
//下面这段没有效果,我想根据参数做筛选,用linq,ShowChartDetail是List<Chartdetail>类型,在静态方法里不能用吗?
//from chartdetail in ShowChartDetail where pname = username
请尝试 var C=(from chartdetail in ShowChartDetail where pname = username select chartdetail).toList();
爱吃西瓜的小强[mcpd]
- 已建议为答案 Kangqiang Hua 2012年5月2日 7:06
- 已标记为答案 Allen_MSDNModerator 2012年5月7日 6:50
全部回复
-
linq的使用跟Static 方法没有关系
//下面这段没有效果,我想根据参数做筛选,用linq,ShowChartDetail是List<Chartdetail>类型,在静态方法里不能用吗?
//from chartdetail in ShowChartDetail where pname = username
请尝试 var C=(from chartdetail in ShowChartDetail where pname = username select chartdetail).toList();
爱吃西瓜的小强[mcpd]
- 已建议为答案 Kangqiang Hua 2012年5月2日 7:06
- 已标记为答案 Allen_MSDNModerator 2012年5月7日 6:50
-
您好,正如@Kangqiang Hua所说,这不是static的问题。首先您的Linq语句中并没有select语句,需要加上select语句制定您要查询的项目。其次,Linq语句是需要用一个变量来接收的,类型为IEnumerable<T>,且此查询语句并不会立刻执行,只有在你调用的时候才会真正执行,如调用了ToList()方法或用了Foeach循环语句。请参考Kangqiang Hua为您修改后的语句。
Allen Li [MSFT]
MSDN Community Support | Feedback to us