Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to use the LINQ Dynamic Query Library posted here - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

The following works great:

List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Where(cus=>cus.CompanyName.Contains("A")).Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");


However if I omit the "Where" clause and do something like this

XML
List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");


I get the following error:

CSS
'new' cannot be resolved into a valid type constructor or function., near function, method or type constructor


What am I missing here?
Posted
Comments
dasblinkenlight 26-Jan-11 20:14pm    
Are you certain that ctx.Customers has at least one customer with a company name containing capital "A"?..
cranialsurge 26-Jan-11 20:22pm    
@dasblinkenlight: Yea, which is why the first code block returns those results. The second one should return everything, but I get the error I posted above instead.

Got a reply to my question on the MSDN forums:

http://goo.gl/KwikB
 
Share this answer
 
I tried both your examples, and they completed fine. The first query returned 75 rows; the second one returned 91 rows. I downloaded samples from Scott's page, opened his DynamicQuery project, and modified the Main() as follows:
C#
//Copyright (C) Microsoft Corporation.  All rights reserved.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Linq.Dynamic;
using System.Windows.Forms;
using NorthwindMapping;

namespace Dynamic
{
    class Program
    {
        static void Main(string[] args)
        {
            Northwind db = new Northwind("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI;"); 
            List<string> paramsList = new List<string> { "CustomerID" };
            var customer =
                db.Customers.
                //Where(cus=>cus.CompanyName.Contains("A")).
                Select("new(" +
                                     string.Join(", ", paramsList.ToArray()) +
                                     ")");
            var count = 0;
            foreach (var x in customer) {
                Console.WriteLine(x);
                count++;
            }
            Console.WriteLine("Returned {0} customers", count);
            Console.ReadLine();
        }
    }
}
This code ran to completion with no errors, with Where clause commented and uncommented.

Are we running different versions of his System.Linq.Dynamic? I was unable to locate the code that would produce the error message that you reported in Scott's implementation.
 
Share this answer
 
Comments
cranialsurge 27-Jan-11 12:06pm    
I'm using the Entity Framework in place of LinqToSql. Do you think that should make a difference?
dasblinkenlight 27-Jan-11 12:27pm    
That's an interesting twist. In theory, I don't think it should make a difference. However, in practice I found the implementations of the query mechanisms of these two frameworks to have a number of small but visible differences. We are in the process of migrating a prototype that was built with Linq2Sql to a production system based on EF, and we are discovering such small differences nearly every day.

It would be interesting to see the top of the crash stack of the example that breaks in your case. Is EF anywhere on the stack, or is it just System.Linq.Dynamic?
cranialsurge 27-Jan-11 12:35pm    
Yup, it sure is. I will need to find out of that library has been modified to be used with the EF. Thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900