C# 在视图中使用Contains()检查IEnumerable
我试图呈现一个链接列表,图标应该根据项目Id是否在C# 在视图中使用Contains()检查IEnumerable,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,我试图呈现一个链接列表,图标应该根据项目Id是否在IEnumerable中找到而改变 这是我目前观点的相关部分: @{ if (product.InFrontPages.Contains(item.ParentCategory.Id)) { <span class="glyphicon glyphicon-checked"></span> } else { <span class="glyphicon
IEnumerable
中找到而改变
这是我目前观点的相关部分:
@{
if (product.InFrontPages.Contains(item.ParentCategory.Id))
{
<span class="glyphicon glyphicon-checked"></span>
}
else
{
<span class="glyphicon glyphicon-unchecked"></span>
}
}
@{
if(product.InFrontPages.Contains(item.ParentCategory.Id))
{
}
其他的
{
}
}
这会导致编译时错误:
“IEnumerable”不包含“Contains”的定义,而最佳扩展方法重载“ParallelEnumerable.Contains(ParallelQuery,int)”需要“ParallelQuery”类型的接收器
我想我可能想要实现,但我还没有想出如何实现。我不明白Jon建议实现通用接口是什么意思
涉及的视图模型:
public class ViewModelProduct
{
public int Id { get; set; }
public string Title { get; set; }
public string Info { get; set; }
public decimal Price { get; set; }
public int SortOrder { get; set; }
public IEnumerable<FrontPageProduct> InFrontPages { get; set; }
public IEnumerable<ViewModelCategoryWithTitle> Categories { get; set; }
}
public class ViewModelProductCategory
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public string ProductCountInfo
{
get
{
return Products != null && Products.Any() ? Products.Count().ToString() : "0";
}
}
public IEnumerable<FrontPageProduct> FrontPageProducts { get; set; }
public ViewModelProductCategory ParentCategory { get; set; }
public IEnumerable<ViewModelProductCategory> Children { get; set; }
public IEnumerable<ViewModelProduct> Products { get; set; }
}
公共类ViewModelProduct
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共字符串信息{get;set;}
公共十进制价格{get;set;}
公共int排序器{get;set;}
公共IEnumerable InFrontPages{get;set;}
公共IEnumerable类别{get;set;}
}
公共类ViewModelProductCategory
{
公共int Id{get;set;}
public int?ParentId{get;set;}
公共字符串标题{get;set;}
公共int排序器{get;set;}
公共字符串ProductCountInfo
{
得到
{
返回产品!=null&&Products.Any()?Products.Count().ToString():“0”;
}
}
公共IEnumerable FrontPageProducts{get;set;}
公共视图模型ProductCategory父类别{get;set;}
公共IEnumerable子项{get;set;}
公共IEnumerable乘积{get;set;}
}
问题在于Contains
LINQ方法没有您期望的签名-您正在尝试检查IEnumerable
是否包含int
。。。它不能,因为它只有FrontPageProduct
引用
我猜你想要的是:
if (product.InFrontPages.Any(page => page.Id == item.ParentCategory.Id)
(我可能会使用条件运算符而不是
if
语句,但这是另一回事。)问题是包含的LINQ方法没有您期望的签名-您正在尝试检查IEnumerable
是否包含int
。。。它不能,因为它只有FrontPageProduct
引用
我猜你想要的是:
if (product.InFrontPages.Any(page => page.Id == item.ParentCategory.Id)
(我可能会使用条件运算符而不是if
语句,但这是另一回事。)一种方法是使用lamda表达式。像这样的
@{
if (product.InFrontPages.Contains(c => c.ParentCategory.Id == item.ParentCategory.Id))
{
<span class="glyphicon glyphicon-checked"></span>
}
else
{
<span class="glyphicon glyphicon-unchecked"></span>
}
}
@{
if(product.InFrontPages.Contains(c=>c.ParentCategory.Id==item.ParentCategory.Id))
{
}
其他的
{
}
}
一种方法是使用lamda表达式。像这样的
@{
if (product.InFrontPages.Contains(c => c.ParentCategory.Id == item.ParentCategory.Id))
{
<span class="glyphicon glyphicon-checked"></span>
}
else
{
<span class="glyphicon glyphicon-unchecked"></span>
}
}
@{
if(product.InFrontPages.Contains(c=>c.ParentCategory.Id==item.ParentCategory.Id))
{
}
其他的
{
}
}
@mjwills我添加了相关的视图模型。@mjwills我在.Count()?
:“运算符”不能应用于int类型的操作数。使用返回产品?.Count().ToString()??“0”
@mjwills我添加了相关的视图模型。@mjwills我在.Count()?
上出错:“运算符”不能应用于int类型的操作数。使用返回乘积?.Count().ToString()?”??“0”
;