Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert lambda expression to type int because it is not a delegate type

Tags:

c#

lambda

linq

Can anyone tell me what is wrong with my the following, and how to fix it:

        //filterItem.Value is a string array - It is being conerted into an int array
        var intFilters = Array.ConvertAll(filterItem.Value, s => int.Parse(s));

        //Returns an IQueryable<int> for all items in this context that are also in intFilters
        var ids = Context.table1.Where(a => intFilters.Any(y => y == a.domId)).Select(x => x.domId);

        //query is an IQueryable
        query = query.Where(x => specUnidsNullable.Contains(y => y == x.Id));

The aim of the above is to get just the records from query that are also contained in ids

I get the following error:

Cannot convert lambda expression to type int because it is not a delegate type

I have also tried this:

var ids = idsNullable.Where(x => x > 0);

Which works fine - the reason I did this was to see if the problem was that ids could not be converted into a lambda type expression.

There are many questions about this error, but none seem to address my particular issue that I can see...

like image 231
Alex Avatar asked Sep 11 '25 21:09

Alex


1 Answers

specUnidsNullable.Contains(y => y == x.Id) calls LINQ Contains method which expects single value of given type (int I suppose in this case), and you are passing expression there. So change to either

specUnidsNullable.Contains(x.Id);

or

specUnidsNullable.Any(y => y == x.Id);
like image 76
Evk Avatar answered Sep 14 '25 10:09

Evk