Reputation: 1809
I'm trying First filter list and then using OrderBy
but I'm getting following error on the Where
clause
Operator '&&' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable'
What's wrong with my query?
Offer internetOffer = offerList
.Where(x => (x.VerticalType == VerticalType.HighSpeedInternet)
&& (x.FeatureList
.Where(y => y.FeatureName == Const.CommonConstants.DOWNLOAD_SPEED_FEATURE_NAME)))
.OrderByDescending(y => y.Value);
Upvotes: 2
Views: 7817
Reputation: 727047
x.FeatureList.Where(...)
produces an IEnumerable<T>
or IQueryable<T>
of items in FeatureList
that match the criteria. Since you are looking for a condition to see if there are any such items, use x.FeatureList.Any(...)
instead:
Offer internetOffer = offerList
.Where(x =>
(x.VerticalType == VerticalType.HighSpeedInternet)
&& (x.FeatureList.Any(y => y.FeatureName ==
Const.CommonConstants.DOWNLOAD_SPEED_FEATURE_NAME))
).OrderByDescending(y => y.Value);
can you please tell how can I get the item from this list whose
FeatureName
isConst.CommonConstants.DOWNLOAD_SPEED_FEATURE_NAME
and have the Max value of propertyValue
?
Add FirstOrDefault
to the call above:
Offer bestInternetOffer = offerList
.Where(x =>
(x.VerticalType == VerticalType.HighSpeedInternet)
&& (x.FeatureList.Any(y => y.FeatureName ==
Const.CommonConstants.DOWNLOAD_SPEED_FEATURE_NAME))
)
.OrderByDescending(y => y.Value)
.FirstOrDefault();
The OrDefault
part will protect your code from crashing when none of the offers has a feature named DOWNLOAD_SPEED_FEATURE_NAME
.
Upvotes: 7
Reputation: 46005
The problem is that
x.FeatureList.Where(y => y.FeatureName == Const.CommonConstants.DOWNLOAD_SPEED_FEATURE_NAME)
returns a IEnumerable<Feature>
instead of a boolean
value.
assuming you want to check if there exists Any()
you have to replace it by
x.FeatureList.Any(y => y.FeatureName == Const.CommonConstants.DOWNLOAD_SPEED_FEATURE_NAME)
Upvotes: 1
Reputation: 216353
Where returns an IEnumerable not a boolean, simply replace Where with Any
... x.FeatureList.Any(y => y.FeatureName == Const.CommonConstant.DOWNLOAD_SPEED_FEATURE_NAME)))....
Upvotes: 2