C# 引用对象和字符串数组的Linq查询
我在将以下代码转换为使用LINQ时遇到一些问题C# 引用对象和字符串数组的Linq查询,c#,.net,linq,C#,.net,Linq,我在将以下代码转换为使用LINQ时遇到一些问题 int occurs = 0; foreach (string j in items) { if (!string.IsNullOrEmpty(j)) { WorkflowModule tempWM = new WorkflowModule(j); if (tempWM.StateID == item.StateID) {
int occurs = 0;
foreach (string j in items)
{
if (!string.IsNullOrEmpty(j))
{
WorkflowModule tempWM = new WorkflowModule(j);
if (tempWM.StateID == item.StateID)
{
occurs++;
}
}
}
return occurs;
到目前为止,我已经:-
var lstItems = (from lstItem in items
where !string.IsNullOrEmpty(lstItem)
let objWorkflowModule = new WorkflowModule(lstItem)
select new
{
tempWM = objWorkflowModule.StateID
}).Where(item.StateID == tempWM));
return lstItems.Count();
但是intellisense不喜欢“.Where(item.StateID==tempWM)”这一行
有人能帮我做到这一点吗
谢谢。使用方法语法时,需要在
Where
运算符上使用lambda:
...
}).Where(x => x.tempWM == item.StateID));
换句话说,您需要“声明”变量
x
,该变量保存查询前一部分的结果。使用方法语法时,您需要在中使用lambda
运算符:
...
}).Where(x => x.tempWM == item.StateID));
换句话说,您需要“声明”变量
x
,该变量保存查询前一部分的结果。它看起来不像是在语句中的任何地方初始化了项。它看起来不像是在语句中的任何地方初始化了项。我是这样做的
var lstItems = from lstItem in items
where !string.IsNullOrEmpty(lstItem)
let objWorkflowModule = new WorkflowModule(lstItem)
select objWorkflowModule.StateID;
return lstItems.Count(t=> t == item.StateID);
我假设item是在您提交的原始代码之外定义的变量。基本上,您不需要在查询中创建匿名类,您可以将谓词放在you'sWhere
中的Count
中。但正如其他人所说,主要的问题是您需要将谓词表示为lambda。以下是我的方法
var lstItems = from lstItem in items
where !string.IsNullOrEmpty(lstItem)
let objWorkflowModule = new WorkflowModule(lstItem)
select objWorkflowModule.StateID;
return lstItems.Count(t=> t == item.StateID);
我假设item是在您提交的原始代码之外定义的变量。基本上,您不需要在查询中创建匿名类,您可以将谓词放在you's
Where
中的Count
中。但正如其他人所说,主要问题是您需要将谓词表示为lambda。我认为他们实际上想要Where(t=>t.tempWM==item.StateID)
因为item似乎是在原始代码之外定义的变量。我想他们实际上想要Where(t=>t.tempWM==item.StateID)
因为项似乎是在原始代码之外定义的变量。