【问题标题】:Linq statement with Contains and Array包含和数组的 Linq 语句
【发布时间】:2018-03-27 14:23:32
【问题描述】:

我有以下 foreach 语句,它从我的数组 (customerConfigTypes) 中返回所有值。 我只想返回特定的值。

我想要做的是,如果我的 Array(customerConfigTypes) 的值为“A”而我的模型(m.ConfigurationType) 的值为 A,我只想返回该特定类型。

因此,在最好的上下文中使用“包含”这个词...。最好的方法是什么?

 var customerConfigTypes = new 
 Models.ReportConfiguration.ReportConfigurationType[]
     {
               ReportConfigurationType.CustomerFlagReport,
               ReportConfiguration.ReportConfigurationType.SalesReport,

     };

@foreach (var vm in Model.Where(m =>customerConfigTypes.Contains(m.ConfigurationType) && 
  m.ReportConfigurationId > 0 ))

       {

   <div class="title-card @if (!hasCreateReportPermission) {<text> disabled</text> }">
               <a class="inner" href="@Url.Action("Index", "ReportsAngular", new { area = "Angular", ReportType="CustomerFlagReport", ConfigurationId=vm.ReportConfigurationId, CanConfigureReports = hasCreateReportPermission })#/">
                <i class="fa fa-wrench"></i>
                <h3 class="panel-title">@vm.ConfigurationName</h3>
                <p>@vm.ConfigurationDescription</p>

            </a>
        </div>

这是我的模型布局

public class ReportConfigurationViewModel : IReportConfigurationViewModel, IXmlSerializable
{
    public int? ReportConfigurationId { get; set; }
    public string ConfigurationName { get; set; }
    public string ConfigurationDescription { get; set; }
    public bool IsTemplate { get; set; }
    public ReportConfigurationType ConfigurationType { get; set; }
    public String ReportTitle { get; set; }
    public virtual IEnumerable<IFilterViewModel> Filters { get; set; }
    public IEnumerable<IFilterViewModel> SelectedFilters { get; set; }
    public IEnumerable<IReportingColumnViewModel> SelectedColumns { get; set; }
    public IEnumerable<IReportSummaryColumnViewModel> SelectedReportSummaryColumns { get; set; }
    public IEnumerable<IReportGroupingColumnViewModel> SelectedReportGroupingColumns { get; set; }
    public IEnumerable<IReportSortingColumnViewModel> SelectedReportSortingColumns { get; set; }
    public string ModeType { get; set; }
    public bool SuppressReportSummaryTitles { get; set; }
    public bool SuppressPageSummaryTitles { get; set; }
    public bool SuppressGroupSummaryTitles { get; set; }
    public System.Xml.Schema.XmlSchema GetSchema() { return null; }

【问题讨论】:

  • 如果找到多个匹配项怎么办?
  • 请发布您的模型布局
  • 如果您只期待一个结果,为什么还要使用foreach
  • 因为我的模型可以有多个我需要在剃刀语法中显示的那种类型的结果。

标签: c# linq model-view-controller


【解决方案1】:

你正在做相反的事情。您不必使用Contains。看看类型是否匹配,Where 子句会带你回到集合。

foreach (var vm in customerConfigTypes.Where(m => m == Model.ConfigurationType))
{
    // do something
}

用 FIDDLE 更新:

Here is a working Fiddle of your exact model

更新模型列表

您需要在模型中使用List&lt;T&gt;。这会让你实现你想要的。这将允许您获取您的视图模型并与提供给视图的customerConfigurationTypes 列表交叉引用它们。

类更新

public class TestVm
{
    public ReportConfigurationType ConfigType { get; set; }   
    public string Name { get; set; }
}

public class ConfigurationViewModel
{
    public List<TestVm> ReportConfigurations { get; set; }
}

代码更新

var results = model.ReportConfigurations.Where(m => customerConfigTypes.Contains(m.ConfigType));

foreach (var result in results.Where(m => m.ReportConfigurationId > 0))
{
    <p>@result.ConfigurationDescription</p>
}

Working Fiddle with updated example

【讨论】:

  • 我不能使用操作数 == 来比较枚举和数组。我以前试过这个。
  • 嗯?这不是将枚举与数组进行比较.....您正在将 ReportConfigurationTypeReportConfigurationType 进行比较......
  • customerConfigTypes 是一个 ReportConfigurationType 数组。 Model.ConfigurationType 是一个 ReportConfigurationType。 customerConfigTypes.Where 将值 (ReportConfigurationTypes) 与模型的 ConfigurationType 属性(同样是 ReportConfigurationType)进行比较。
  • @Stacey 我更新了答案,使用您的Enumarray 提供了一个带有工作小提琴的链接,向您展示如何比较这些值。
  • 感谢您提供的信息,现在唯一的问题是我无法将视图模型的值传递回剃刀语法。假设我想这样做。

    @vm.ConfigurationDescription

猜你喜欢
  • 2013-12-19
  • 2017-01-21
  • 2016-01-07
  • 1970-01-01
  • 2017-10-17
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多