0

How do I get the count of a column in a list in foreach loop

Model

public class Region
{
    public int LanguageId { get; set; }
    public string LanguageName { get; set; }
}

View Model

public class ViewModel
{
    public List<Region> Regions { get; set; }
}

Data

 0 LanguageId = 1 LanguageName = "English"
 1 LanguageId = 2 LanguageName = "German"
 2 LanguageId = 3 LanguageName = "French"
 3 LanguageId = 1 LanguageName = "English"
 4 LanguageId = 1 LanguageName = "English"

I want the count of LanguageId if its greater than 1 than I want to apply some condition

View

foreach (var item in Model.Regions)
{   
    if (Model.Regions[item].LanguageId.Count > 1)
    {
        // do something
    }
}

The requirement is to display

English (apply condition here because there are more that one)
French
German
alexbt
  • 16,415
  • 6
  • 78
  • 87
Dave
  • 263
  • 6
  • 23
  • What do you mean _count of LanguageId_? `LanguageId` is a property (typeof `int`, not a collection - there is nothing to 'count'). Do you mean you want to test that the value of `LanguageId` is greater than 1? Or do you want to count the number of items in the collection where `LanguageId == 1`? –  Apr 15 '16 at 22:49
  • I want the count of number of items in the collection where LanguageId == 1. Have edited my question. – Dave Apr 16 '16 at 03:20
  • Still not clear. Do you want the view to be `English - 3`, `German - 1`, `French - 1` - i.e. distinct values with the count of them next to it? (and what is the `Subject : C#` etc you have shown and how does that relate to `Regions`) –  Apr 16 '16 at 03:29
  • Making it more clear Language : English,French,German. This is what I want and this is what I am getting Language : English, Language : French, Language : German, – Dave Apr 16 '16 at 03:33
  • Please see my recent Edit. – Dave Apr 16 '16 at 03:36
  • OK, then you just need a `.GroupBy()` clause in your controller (and you will need a view model to include a `Count` property so you can apply the condition in your view. Give me 20 min and I can add an answer if you don't understand –  Apr 16 '16 at 03:39
  • Please it would be helpful. – Dave Apr 16 '16 at 03:56

1 Answers1

1

First you will need a view model for Region that includes the Name and Count properties

public class RegionVM
{
    public string Name { get; set; }
    public int Count { get; set; }
}

and then in the controller use a .GroupBy() to group the regions by name

IEnumerable<Region> regions = .... // your query to get the regions
IEnumerable<RegionVM> model = regions.GroupBy(x => x.LanguageName).Select(x > new RegionVM()
{
    Name = x.Key,
    Count = x.Count()
}.AsEnumerable();
return View(model)

and in the view

@model IEnumerable<RegionVM>
@foreach(var language in Model)
{
    <div>@language.Name</div>
    if(language.Count > 1)
    {
        // do something
    }
}
  • I appreciate your time and answer would like to know why are we using View models here as no multiple tables are used. – Dave Apr 16 '16 at 17:36
  • What is the logic behind using Name = x.Key, Count = x.Count() – Dave Apr 16 '16 at 17:46
  • 1
    Using a view model has nothing to do with using multiple tables. The purpose of a view model is to represent the data we need in the view, and every view should have a view model specific to it. Refer also [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Apr 16 '16 at 23:42
  • In your case you need a view model to display the name of the language (`Name` property) and a property for the number of that language (`Count`) so your can preform you conditional view logic. –  Apr 16 '16 at 23:44