Stacey
Stacey

Reputation: 63

Linq statement with Contains and Array

I have the following foreach statement and it return all the value from my array(customerConfigTypes). I only want to return only specific value.

What I'm trying to do is if my Array(customerConfigTypes) has a value "A" and my model(m.ConfigurationType) is of value A I only want to return that specific type.

So the word Contain doesn't seen to be used in the best context.... What will be the best way to do this?

 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>

Here is my model layout

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; }

Upvotes: 0

Views: 99

Answers (1)

ragerory
ragerory

Reputation: 1378

You're doing it in reverse. You don't have to use Contains. Just see if the types match and the Where clause will bring you back the set.

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

UPDATE WITH FIDDLE:

Here is a working Fiddle of your exact model

UPDATE WITH LIST OF MODEL

You'll want to use a List<T> in your model. This will get you to achieve what you want. This will allow you to take your viewmodels and cross reference them with a list of customerConfigurationTypes supplied to the View.

Class Update

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

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

Code Update

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

Upvotes: 1

Related Questions