【发布时间】:2017-01-20 02:39:14
【问题描述】:
我有一个要过滤的广告列表。广告只有一个内容评级:G、PG、PG13 或 R。现在,如果我指定一个内容评级,我有一种方法可以过滤列表。例如,如果我将“G”传递到我的方法中,我会返回列表中只有“G”内容评级的项目。
如果我要提供两个内容分级,例如“G”和“PG”,该方法不会返回任何内容,因为它正在寻找内容分级为“G”和“PG”的广告,而此类广告不存在,因为每个广告只关联一个内容分级。
我想重写我的方法,这样如果我传入两个内容评级,比如“G”和“PG”,它将返回具有“G”或“PG”的广告列表。
这是我的方法中需要解决的部分:
adList = adList.Where(ad => ad.IsG == filter.ContentRatings.IsG
&& ad.IsPG == filter.ContentRatings.IsPG
&& ad.IsPG13 == filter.ContentRatings.IsPG13
&& ad.IsR == filter.ContentRatings.IsR);
edit:filter 是一个类,ad.IsPG 和 filter.ContentRatings.IsG (IsPG13, IsR, IsG) 是布尔值。如果您想知道,这是行不通的。
adList = adList.Where(ad => ad.IsG == filter.ContentRatings.IsG
|| ad.IsPG == filter.ContentRatings.IsPG
|| ad.IsPG13 == filter.ContentRatings.IsPG13
|| ad.IsR == filter.ContentRatings.IsR);
【问题讨论】:
-
你能分享更多你目前的方法吗?参数只是作为字符串输入吗?您提供的部分中使用的参数如何?
-
为什么不试试标题中提到的
||。在这种情况下filter是什么?它是在哪里定义的?
标签: c# methods filter where operator-keyword