我有以下声明:
return articles.Select(entity => new OrderCateringArticleViewModel
{
ArticleId = entity.Id, ImagePath = entity.ImagePath
})
我想通过NOT来限制结果,包括值HideUntilDate!= null和HideUntilDate>的项目。 today'date。根据我之前发布的另一个问题,我想的是:
return articles.Select(entity => new OrderCateringArticleViewModel
{
ArticleId = entity.Id, ImagePath = entity.ImagePath
}).Where(!(entity.HideUntilDate != null && entity .HideUntilDate.Value > DateTime.Today));
这似乎不起作用,在where子句中不识别实体。任何人吗?
答案 0 :(得分:3)
LINQ方法接受委托(或表达式,如果你正在进行查询)。
LINQ方法链中一个方法的委托不会以任何方式转移到链中的下一个方法。您需要使用委托语法编写每个方法:
return articles
.Select(entity => new OrderCateringArticleViewModel
{
ArticleId = entity.Id, ImagePath = entity.ImagePath
})
.Where(entity => !(entity.HideUntilDate != null && entity .HideUntilDate.Value > DateTime.Today));
请注意,在entity =>
的调用中添加了.Where
。
这是必要的。编译器不会将此委托参数从Select
方法转移到下一步。
但是,现在这会给您带来另一个错误,entity
没有HideUntilDate
。那是为什么?
因为Where
委托对.Select
的结果进行操作,ArticleId
是一个新的匿名对象,只有ImagePath
和return articles
.Where(entity => !(entity.HideUntilDate != null && entity .HideUntilDate.Value > DateTime.Today))
.Select(entity => new OrderCateringArticleViewModel
{
ArticleId = entity.Id, ImagePath = entity.ImagePath
});
。
幸运的是,修复很简单,切换两种方法的顺序:
<p:column id="expenseTypeExpenseCol" headerText="Expense Type">
<p:selectOneMenu id="expenseTypes"
value="#{expenseSheet.selectedExpenseType}">
<f:selectItem itemLabel="Select Expense Type" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{expenseSheet.expenseTypeItems}" />
<p:ajax event="change"
listener="#{timeAndExpenseManagedController.populateExpensePoNumber}"
update="expenseSheet" />
</p:selectOneMenu>
</p:column>
<p:column id="bestellungExpenseCol" headerText="Purchase Order">
<p:selectOneMenu id="bestelungExpenseSheet"
value="#{expenseSheet.selectedPoNumber}">
<f:selectItem itemLabel="Select PoNumber" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{expenseSheet.expSelectItems}" />
<p:ajax event="change"
listener="#{timeAndExpenseManagedController.populateExpensePos}"
update="expenseSheet" />
</p:selectOneMenu>
</p:column>
<p:column id="expensePosCol" headerText="Pos.">
<p:selectOneMenu id="expensePos"
value="#{expenseSheet.lineItem}" />
<f:selectItem itemLabel="Select Pos" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{expenseSheet.poLineItemFormList}"
var="poLine" itemLabel="#{poLine.lineItem}"
itemValue="#{poLine.lineItem}" />
<p:ajax event="change"
listener="#{timeAndExpenseManagedController.poExpenseValueChanged}"
update="expenseSheet" />
</p:selectOneMenu>
</p:column>
这将在构造匿名对象之前进行过滤。
答案 1 :(得分:1)
尝试以下代码
return articles.Where(!(entity.HideUntilDate != null && entity .HideUntilDate.Value > DateTime.Today)).Select(entity => new OrderCateringArticleViewModel
{
ArticleId = entity.Id,
ImagePath = entity.ImagePath
});