C# LINQ查询中使用函数的Where子句
我有一段代码:C# LINQ查询中使用函数的Where子句,c#,linq,C#,Linq,我有一段代码: IList<Opportunity> filteredOpportunityProperties = new List<Opportunity>(); List<LookupWithIntId> selectedProperties = opportunityFilter.PropertyTypes; List<string> propertyTypes = selectedProperties.Select(item =>
IList<Opportunity> filteredOpportunityProperties = new List<Opportunity>();
List<LookupWithIntId> selectedProperties = opportunityFilter.PropertyTypes;
List<string> propertyTypes = selectedProperties.Select(item => item.Name).ToList();
opportunities.Where((item) =>
{
string productType = item.Properties[0].ProductType;
bool propertyMatch = propertyTypes.Any(propTypes => productType.Contains(propTypes));
if (propertyMatch) select item;
});
IList filteredOpportunityProperties=new List();
列出selectedProperties=opportunityFilter.PropertyTypes;
List propertyTypes=selectedProperties.Select(item=>item.Name).ToList();
机会。其中((项目)=>
{
字符串productType=item.Properties[0]。productType;
boolpropertyMatch=propertyTypes.Any(propTypes=>productType.Contains(propTypes));
如果(propertyMatch)选择项目;
});
如果条件匹配,我希望选择该项。但是,我得到了一个错误:
嵌入语句不能是声明或标记语句
任何建议 在where子句中,更改此行:
if(propertyMatch) select item;
为此:
return propertyMatch;
如果谓词结果为true,where子句将返回项,因此您只需返回布尔结果。在where子句中,更改此行:
if(propertyMatch) select item;
为此:
return propertyMatch;
如果谓词结果为true,where子句将返回该项,因此您只需返回布尔结果。哇,这太疯狂了!我不知道你能和linq一起做这件事!哇,这太疯狂了!我不知道你能和linq一起做这件事!