Using "Single" in Dynamic Linq

I am trying to convert a Linq query that I worked in Linq to be able to work in dynamic linq (using System.Linq.Dynamic), because I want the user to be able to create their own queries and this query string will be added to other query strings at runtime.

I have a request:

db.incidents.Where(a => a.incidentLocations.Single().location.street.Contains(location);

and I tried converting it to the following dynamic linq line:

query = 
string.Concat("incidentLocations.Single().location.street.Contains(\"", location, "\")");

db.incidents.Where(query);

Where location is a string that contains search text.

I managed to convert all my other queries to dynamic linq, but this one I am struggling with an exception error:

"No applicable aggregate method" Single "exists"

, linq , - , .

+3
3

Linq.Dynamic, Where, , . , Single First .., , dev, ;)

EDIT: , :

public static object Single(this IQueryable source)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.Provider.Execute(
            Expression.Call(
                typeof(Queryable), "Single",
                new Type[] { source.ElementType },
                source.Expression));
    }
+7

, , , !

First()/FirstOrDefault() Linq to Entities Linq to SQL, , @Guillaume86 !

MicrosoftDynamic.sql: DynamicQueryable:

    public static object FirstOrDefault(this IQueryable source)
        {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.Execute(
                Expression.Call(
                    typeof(Queryable), "FirstOrDefault ",
                    new Type[] { source.ElementType },
                    source.Expression));
        }

IEnumerableSignatures

            void FirstOrDefault();

( FirstOrDefault, First() , linq )

:)

+3

Have you tried this?

db.incidents.Where(incidentLocations.Single().location.street.Contains(location));
0
source

Source: https://habr.com/ru/post/1788952/


All Articles