简体   繁体   中英

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???

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM