Share via


How to get assembly for System.LINQ.Dynamic

Question

Tuesday, January 28, 2014 5:32 PM

I am building an MVC project using jqgrid.  This compile of this project is delivering two errors "The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<tsource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly." and "No overload for method 'Where' takes 2 arguments".  I understand that this is due to the failure to make a using for System.Linq.Dynamic.  But when I tried to add such a using, I was told that there was no such assembly.  </tsource>

I was under the impression that Dynamic LINQ was included in VS 2012.  If that is not correct, where can I get this assembly to add to my bin.  I was told it was supposed to be on Scott Gu blog page.  However although there were numerous examples, that assembly was not included.

All replies (2)

Tuesday, January 28, 2014 6:10 PM âś…Answered

Could you post the code that you are currently trying to use? Dynamic LINQ, which was reference by Scott Guthrie's Dynamic.cs code in the blog post that you mention defines the actual System.Dynamic.Linq assembly. Basically all you need to do is download it through one of the following methods and you should be able to use it without issue :

  • System.Dynamic.Linq NuGet Package - You can easily include this within your application by simply right-clicking on your Project, Choosing Manage NuGet Packages and searching for System.Dynamic.Linq and including it.
  • Download it from this Sample Project - You'll find the necessary files are included within this sample project.
  • Download it directly from github - The github project contains both the source code and DLL file that you can reference (be sure to add it to your project under the Add References option).

Dynamic LINQ Documentation

As far as actual documentation, I think your best bet might be to actually look at the available source for the System.Linq.Dynamic library, which is available below : 

You can find a few resources that might help with dynamic queries as well :


Wednesday, January 29, 2014 9:50 AM

I have been using Web Forms since we first converted from classic Asp in 2004. This is my first foray into MVC so naturally I went for the most current version that we had i.e. MVC 4 with EF 5 in framework 4.5 using VS 2012 using 4.0.30319 version of System.Web.Razor. So I may be missing something.

 This is one function

        /// <summary>
        /// Converts a queryable expression into a format suitable for the JqGrid component, when 
        /// serialized to JSON. Use this method when returning data that the JqGrid component will 
        /// fetch via AJAX. Take the result of this method, and then serialize to JSON. This method 
        /// will also apply paging to the data.
        /// </summary>
        /// <typeparam name="T">The type of the element in baseList. Note that this type should be 
        /// an anonymous type or a simple, named type with no possibility of a cycle in the object 
        /// graph. The default JSON serializer will throw an exception if the object graph it is 
        /// serializing contains cycles.</typeparam>
        /// <param name="baseList">The list of records to display in the grid.</param>
        /// <param name="currentPage">A 1-based index indicating which page the grid is about to display.</param>
        /// <param name="rowsPerPage">The maximum number of rows which the grid can display at the moment.</param>
        /// <param name="orderBy">A string expression containing a column name and an optional ASC or 
        /// DESC. You can, separate multiple column names as with SQL.</param>
        /// <param name="searchQuery">The value which the user typed into the search box, if any. Can be 
        /// null/empty.</param>
        /// <param name="searchColumns">An array of strings containing the names of properties in the 
        /// element type of baseList which should be considered when searching the data for searchQuery.</param>
        /// <param name="userData">Arbitrary data to be returned to the grid along with the row data. Leave 
        /// null if not using. Must be serializable to JSON!</param>
        /// <returns>A structure containing all of the fields required by the JqGrid. You can then call 
        /// a JSON serializer, passing this result.</returns>
        [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Jq",
            Justification = "JqGrid is the correct name of the JavaScript component this type is designed to support.")]
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Jq",
            Justification = "JqGrid is the correct name of the grid component we use.")]
        public static JqGridData ToJqGridData<T>(this IQueryable<T> baseList,
            int currentPage,
            int rowsPerPage,
            string orderBy,
            string searchQuery,
            IEnumerable<string> searchColumns,
            object userData)
        {
            var filteredList = ListAddSearchQuery(baseList, searchQuery, searchColumns);
            var orderedList = (orderBy != null) ? filteredList.OrderBy(orderBy) : filteredList;
            var pagedModel = new PagedList<T>(orderedList, currentPage - 1, rowsPerPage);
            return pagedModel.ToJqGridData(userData);
        }

It errors on line var orderedList = (orderBy != null) ? filteredList.OrderBy(orderBy) : filteredList; with error

The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<tsource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.</tsource>

The next error occurs in this function:

       /// <summary>
        /// Adds a Where to a Queryable list of entity instances.  In other words, filter the list 
        /// based on the search parameters passed.
        /// </summary>
        /// <typeparam name="T">Entity type contained within the list</typeparam>
        /// <param name="baseList">Unfiltered list</param>
        /// <param name="searchQuery">Whatever the user typed into the search box</param>
        /// <param name="searchColumns">List of entity properties which should be included in the 
        /// search.  If any property in an entity instance begins with the search query, it will 
        /// be included in the result.</param>
        /// <returns>Filtered list.  Note that the query will not actually be executed until the 
        /// IQueryable is enumerated.</returns>
        private static IQueryable<T> ListAddSearchQuery<T>(IQueryable<T> baseList, string searchQuery, IEnumerable<string> searchColumns)
        {
            if ((String.IsNullOrEmpty(searchQuery)) | (searchColumns == null)) return baseList;
            const string strpredicateFormat = "{0}.ToString().StartsWith(@0)";
            var searchExpression = new System.Text.StringBuilder();
            string orPart = String.Empty;
            foreach (string column in searchColumns)
            {
                searchExpression.Append(orPart);
                searchExpression.AppendFormat(strpredicateFormat, column, searchQuery);
                orPart = " OR ";
            }
            var filteredList = baseList.Where(searchExpression.ToString(), searchQuery);
            return filteredList;
        }

It errors on line var filteredList = baseList.Where(searchExpression.ToString(), searchQuery); with the error

No overload for method 'Where' takes 2 arguments

A search on the web for these two error messages reported that this was due to the failure to include using a using for System.Linq.Dynamic;  The code came from a project that I downloaded which came from http://blogs.teamb.com/craigstuntz/2009/04/17/38229/ Using jqGrid with ASP.NET MVC.  This project was in an older pre-razor version of MVC and I was trying to update it.  he had a using System.Linq.Dynamic on his class.  However every attempt I made to place that same using in my class failed with

The type or namespace name 'Dynamic' does not exist in the namespace 'System.Linq' (are you missing an assembly reference?)  I went to Scott Gu's Blog to get the assembly, but the available downloads did not contain the assembly as far as I could tell. 

Update: I opened Nuget Package manager.  After about 45 minutes of searching I located System.Linq.Dynamic for 4.0 .Net.  Hopefully there are no changes for 4.5.  The project now compiles properly.  However I am concerned about the reference.  While the assembly is located in my bin, the reference states it is referencing from the packages directory of the Visual Studio 2012 folder.  Will this be a problem upon deployment?