Use of "Single" in Dynamic Linq

I am trying to convert a Linq query that I have working in Linq to be able to work in dynamic linq (using System.Linq.Dynamic) because I want a user to be able to form their own queries and this query string would be added onto other query strings at runtime.

I have a query:

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

and I have tried to convert it to the following dynamic linq string:

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

Where location is a string that includes search text.

I have managed to convert all my other queries to dynamic linq but this one i am struggling with the exception error:

"No applicable aggregate method 'Single' exists"

I understand that dynamic linq does not support all extension methods, could someone possibly tell me how I could get round this problem.

-------------Problems Reply------------

Get the source of Linq.Dynamic, copy paste the Where method, change the signature and the string with the function name inside the method and you're good to go. I did it to add Single First etc, I can't copy it here because I'm not on my dev machine but I'll do it later if necessary ;)

EDIT: here's the Single method if you decide to use it:

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

Sorry to digg up a very old thread, but I thought I could add somevaluable information!

I had to do this for First()/FirstOrDefault() with Linq to Entities instead of your Linq to SQL and I can confirm @Guillaume86 's solution sure works!

Here's how I modified MicrosoftDynamic.sql: I added this inside the static DynamicQueryable class:

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

I also modified interface IEnumerableSignatures as

void FirstOrDefault();

(I used FirstOrDefault because First() isn't supported when it's not the last call in linq to entities)

You can repeat this for any supported function :)

Have you tried this?


Category:linq Views:1 Time:2011-01-31
Tags: linq

Related post

  • Dynamic Linq To Sql With ComboBox and Column.Contains 2009-06-25

    I have a text box, combo box, button and DataGridView on a form that is used to search and return customer information from a MSSQL view (vCustomer). It works great, but I know my code can be more efficient. The four items in the combobox represent c

  • Dynamic Linq Library Help 2010-03-28

    I have the following class: public class Item { public Dictionary<string, string> Data { get; set; } } and a list of it: List<Item> items; I need to filter and order this list dynamically using SQL-Like strings. The catch is, that I need

  • Cache delegate of compiled Dynamic Linq Func *without* params? 2010-07-26

    I'm using the Dynamic.ParseLambda method from the Dynamic LINQ library to create expressions, compile each to a Func<>, and cache each in a dictionary: // parse some dynamic expression using this ParseLambda sig: Expression<Func<TArgument

  • Dynamic LINQ GroupBy Multiple Columns 2010-10-14

    I need to translate the following LINQ query to Dynamic LINQ that accepts several grouping columns based on user input. Basically I have a bunch of dropdownlists that apply groupings and I don't want to enumerate every combination of groupings. If Dy

  • Dynamic LINQ help for sorting problem 2010-10-20

    I have a simple IEnumerable collection of Order objects. I want to write a generic sort function that accepts the collection and a sortkey that corresponds to a property on an Order object. I don't want to hardcode the sort instructions for every pro

  • Dynamic.Linq Building generic Where Condition 2010-11-15

    I have been using System.Linq.Dynamic library. Is there a way to use Entities with one to many relationship. To be specific I have 3 classes class A { public int Id {get;set;} ... ... public Class2 class2 {get;set;} public ICollection<Another>

  • c# Dynamic.Linq - groupby and then select by condition 2011-01-09

    I thought it'll be easy but i'm really struggling with this. I have a Table with this schema: Name, Date, Value Here's what I'm trying to achive: group all rows by 'Name' from that group, select the 'Value' which it's 'Date' is the biggest The #1 was

  • Using DateTime in Dynamic LINQ to Entities 2011-02-09

    I am using LINQ to Entities to retrieve item purchase dates as follows: where EntityFunctions.TruncateTime(order.PurchaseDate) == The key here is that the DB column contains the Date and Time so the time must be stripped for the c

  • How can I dynamically select my Table at runtime with Dynamic LINQ 2011-02-17

    I'm developing an application to allow engineers to conduct simple single table/view queries against our databases by selecting Database, Table, Fields. I get how to use the Dynamic LINQ Library Sample to provide for dynamically selecting the Select,

  • Generic function for building Dynamic Linq query of string.contains against tokenized string 2011-05-06

    I am using Expression.And and Expression.Or to build dynamic linq queries. When the property/field being queried is a string, and the string contains spaces I would like to tokenize the string on the spaces and create an "And'd" sub query on the toke

  • Dynamic linq query with multiple/unknown criteria 2011-06-23

    I am looking to implement a system whereby a use that 'build' conditions and then return the resulting data back from the database. At present, there is a stored procedure which generates SQL on the fly and executes it. This is a particular issue tha

  • Use Any() and Count() in Dynamic Linq 2011-06-29

    I am trying to write dynamic Linq Library query to fetch record on condition, Customers who has order count is greater than 3 and ShipVia field equal 2. Below is my syntax what i have tried. object[] objArr = new object[10]; objArr[0] = 1; IQueryable

  • Dynamic Linq queries for multiple runtime filters 2011-07-08

    I have a winforms app that basically loads a bunch of data from a database using entity framework and displays it on a gridview. For this scenario, consider the entity bound to the gridview to be this one: public class Person { public string name; pu

  • LINQ (Dynamic): OrderBy within a GroupBy using dynamic linq? 2011-09-29

    I had the following query using normal linq and it was working great (using anonymous type), var result = from s in Items group s by s.StartTime into groupedItems select new {groupedItems.Key, Items= groupedItems.OrderBy(x => x.Name) }; But using

  • How to use Dynamic LINQ to query expressions on different object types at the same time 2012-01-05

    I have been experimenting with Dynamic LINQ as a solution to a scenario, whereby I need to allow a user to select a series of files based upon a "tag" that is assigned to it. E.g. File A - tagged as "a", File B - tagged as "b", File C - tagged as "c"

  • Dynamic Linq - Perform a query on an object with members of type "dynamic" 2012-01-12

    I am trying to use a dynamic linq query to retrieve an IEnumerable<T> from an object collection (Linq to Object), each of the objects in the collection have an internal collection with another set of objects where the data is stored, these valu

  • Dynamic linq query due to the query string in mvc c# 2012-02-10

    In my controller, I have to test conditions based on the specified query strings that the user passed. These are the conditions that I'm currently testing: string dep = Request.QueryString["dep"]; string cat = Request.QueryString["cat"]; string brand

  • Dynamic linq:Creating an extension method that produces JSON result 2008-09-08

    I'm stuck trying to create a dynamic linq extension method that returns a string in JSON format - I'm using System.Linq.Dynamic and Newtonsoft.Json and I can't get the Linq.Dynamic to parse the "cell=new object[]" part. Perhaps too complex? Any ideas

  • Dynamic LINQ and Dynamic Lambda expressions? 2008-09-22

    What is the best way of dynamically writing LINQ queries and Lambda expressions? I am thinking of applications where the end user can design business logic rules, which then must be executed. I am sorry if this is a newbie question, but it would be g

Copyright (C), All Rights Reserved.

processed in 0.233 (s). 11 q(s)