Reputation: 189
I have 3 tables in my database:
I've also created a ViewModel in MVC 4 with all the tables, but when I'm trying to list the Category Details in the view (.cshtml), the WHERE clause doesn't work as I was expected. I'm trying to list the Language not by key, but by Title.
@foreach (var item in Model.lstCategoryDetails)
{
<tr>
<td>@item.Title</td>
<td>@Model.lstLanguages.Title.Where(x => x.ID == @item.LanguageID)</td>
<td>@item.Description</td>
</tr>
}
Upvotes: 2
Views: 5878
Reputation: 642
you should say
@Model.lstLanguages.Where(x => x.ID == @item.LanguageID).First().Title
you can apply 'where,select etc...' to list, and return type is Ienumerator.
when you apply 'First() or FirstOrDefault()' returns to you element type of your list.
for exm. you have List<int> intList = (1,2,3,4,5,6).
and applying:
intList.where(p=>p == 4 || p== 5).FirstOrDefault();
returns to you '4' as type of integer.
Upvotes: 1
Reputation: 107267
Enumerable.Where
returns another IEnumerable
, even if this contains just one found
item. So replace .Where(
with .FirstOrDefault(
or .Single(
etc.@
on in the lambda filter.Title
property of the selected Language
e.g.
@Model.lstLanguages.FirstOrDefault(x => x.ID == item.LanguageID).Title
Note that you'll get a NRE if the language
isn't found.
Upvotes: 3