user2810643
user2810643

Reputation: 61

How do you make a cast in Lambda expressions?

Taking in account the last is what I want, I get an error on the Select:

AwardsListViewModel viewModel = AwardListViewModel
{
     menuChild = repository
                   .menuChild
                   .Where(p => p.MenuParentAcronym == "Awards Processing" 
                               && p.IsActive == "True")
                   .OrderBy(c => c.DisplayOrder)
                   .Select(m => m.Description == "Awards Processing List")
};

The error is: Cannot implicity convert type System.Linq.IQueryable<bool> to System.Collections.Generic.IEnumerable<AwardsSystem30.Domain.Entities.MenuChild>. An explicit conversion exist (are you missing a cast?)

How do I cast it???

Upvotes: 0

Views: 655

Answers (2)

voidengine
voidengine

Reputation: 2579

I suspect your query doesn't do what you intended - it returns a IQueryable<bool>. I'm guessing the last Select clause should be a Where instead to work as expected.

If that's correct, following code should work (I've combined the where bits into one)

AwardsListViewModel viewModel = AwardListViewModel
{
     menuChild = repository
                   .menuChild
                   .Where(p => p.MenuParentAcronym == "Awards Processing" 
                               && p.IsActive == "True")
                               && p.Description == "Awards Processing List")
                   .OrderBy(c => c.DisplayOrder)
};

Upvotes: 5

Ark
Ark

Reputation: 101

You can cast to List.

AwardsListViewModel viewModel = AwardListViewModel
{
     menuChild = repository
                   .menuChild
                   .Where(p => p.MenuParentAcronym == "Awards Processing" 
                               && p.IsActive == "True")
                   .OrderBy(c => c.DisplayOrder)
                   .Select(m => m.Description == "Awards Processing List")
                   .ToList()
};

Upvotes: -2

Related Questions