0

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; }
4
  • what if multiple matches found ? Commented Mar 27, 2018 at 14:25
  • Please post your model layout Commented Mar 27, 2018 at 14:30
  • Why are you using foreach if you are only expecting one result? Commented Mar 27, 2018 at 14:37
  • Because my model can have multiple result of that type that I need to display in my razor syntax. Commented Mar 27, 2018 at 14:40

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

I can't use the operand == to compare from an enum to an Array.. I tried this before.
Huh? This isn't comparing an enum to an array..... You're comparing ReportConfigurationType to ReportConfigurationType....
customerConfigTypes is an array of ReportConfigurationType. Model.ConfigurationType is a ReportConfigurationType. customerConfigTypes.Where compares the values (ReportConfigurationTypes) to the model's ConfigurationType property (again, a ReportConfigurationType).
@Stacey I updated the answer to provide a link with a working fiddle using your Enum and array to show you how to compare the values.
thank you for the info, the only problem now is I can't pass the value of my view model back into my razor syntax. Let's say if I wanted to do this. <p>@vm.ConfigurationDescription</p>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.