Alex
Alex

Reputation: 3968

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

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

Upvotes: 3

Views: 7781

Answers (2)

drunin
drunin

Reputation: 1

I believe that your issue will be resolved after you install EntityFramework and add using System.Data.Entity.QueryableExtensions.

Upvotes: 0

Evk
Evk

Reputation: 101533

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

Upvotes: 5

Related Questions