Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Dynamic Query and Lambda expression construction [modified]

Dynamic Query and Lambda expression construction [modified]

Scheduled Pinned Locked Moved LINQ
databaselinqhelpcsharpfunctional
13 Posts 3 Posters 35 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gshen

    Any friend can help? I tried many times and failed. I appreciate your kind help. --------------------------------------------------------- using System.Data; using System.Linq; using System.Data.Linq; using System.Linq.Expression; ... //this example uses SQL Northwind database //There are tables in the database: Customers and Orders. //OrderID is a field (property) of Order. //the intended dynamic query lambda expression should be "o=>o.OrderId==10305" //so,in the last line, the value of "lex" should be "o=>o.OrderId==10305" MyDataContext dC = new MyDataContext(); int i = 10305; ParameterExpression pa = Expression.Parameter(typeof(Order), "o"); MemberExpression me = Expression.Property(pa, "OrderID"); ConstantExpression ce = Expression.Constant("i"); Expression ex = Expression.Equal(me, ce); Expression lex = Expression.Lambda<Func<Order, bool>>(ex, new ParameterExpression[] { pa }); IQueryable custs = dC.Customers.Where(c =>c.Orders.Where(lex).First() != null); The compiler gives the error message: (in the last statement, c.Orders.Where(lex) is underlined with red wavy line) Error 1 The type arguments for method 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

    Sheng

    modified on Friday, May 16, 2008 12:20 PM

    E Offline
    E Offline
    Ed Poore
    wrote on last edited by
    #4

    gshen wrote:

    IQueryable custs = dC.Customers.Where(c =>c.Orders.Where(lex).First() != null);

    Try something like:

    var custs = dC.Customers.Where<Order>(c => c.Orders.Where(lex).First() != null);

    Or since I don't have the time at the moment, change the type specifier for the Where statment to what you want to be returned from the query.


    I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

    G 1 Reply Last reply
    0
    • E Ed Poore

      gshen wrote:

      IQueryable custs = dC.Customers.Where(c =>c.Orders.Where(lex).First() != null);

      Try something like:

      var custs = dC.Customers.Where<Order>(c => c.Orders.Where(lex).First() != null);

      Or since I don't have the time at the moment, change the type specifier for the Where statment to what you want to be returned from the query.


      I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

      G Offline
      G Offline
      gshen
      wrote on last edited by
      #5

      Dear Ed: I modified the last statement as you told. But I got three errors: Error 1 'System.Data.Linq.Table<DynamicTry.Customer>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,bool>>)' has some invalid arguments Error 2 Instance argument: cannot convert from 'System.Data.Linq.Table<DynamicTry.Customer>' to 'System.Linq.IQueryable<DynamicTry.Order>' Error 3 Argument '2': cannot convert from 'lambda expression' to 'System.Linq.Expressions.Expression<System.Func<DynamicTry.Order,bool>>'

      Sheng

      E 1 Reply Last reply
      0
      • G gshen

        Dear Ed: I modified the last statement as you told. But I got three errors: Error 1 'System.Data.Linq.Table<DynamicTry.Customer>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,bool>>)' has some invalid arguments Error 2 Instance argument: cannot convert from 'System.Data.Linq.Table<DynamicTry.Customer>' to 'System.Linq.IQueryable<DynamicTry.Order>' Error 3 Argument '2': cannot convert from 'lambda expression' to 'System.Linq.Expressions.Expression<System.Func<DynamicTry.Order,bool>>'

        Sheng

        E Offline
        E Offline
        Ed Poore
        wrote on last edited by
        #6

        Can you provide a sample project which exhibits the problems your trying to fix? It's quite difficult to try and guess the structure of your classes from these snippets.


        I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

        G 1 Reply Last reply
        0
        • E Ed Poore

          Can you provide a sample project which exhibits the problems your trying to fix? It's quite difficult to try and guess the structure of your classes from these snippets.


          I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

          G Offline
          G Offline
          gshen
          wrote on last edited by
          #7

          Ed: The program is as follows: you need a windows form which has a textbox for input, a text box for output, and a button for execute the program. And Also, you need SQL Northwind database to construct a DataContext using Linq to SQL graphical interface. Because the other parts of the windows form and database files are quite messy and confusing (not useful), I just give you the revevant part. Thank you very much! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Data.Linq; using System.Text; using System.Windows.Forms; //using System.Linq.Dynamic; using System.Linq.Expressions; namespace DynamicTry { public partial class DynamicTry : Form { public DynamicTry() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // int i = 5; DynamicDataDataContext ddc = new DynamicDataDataContext(); ParameterExpression pa = Expression.Parameter(typeof(Order), "o"); MemberExpression me = Expression.Property(pa, "OrderID"); ConstantExpression ce = Expression.Constant(10300); Expression ex = Expression.Equal(me, ce); Expression lex = Expression.Lambda<Func<Order, bool>>(ex, new ParameterExpression[] { pa }); IQueryable<Customer> custs = ddc.Customers.Where(c => c.Orders.Where(lex).First() != null); foreach (Customer cust in custs) txtboxOutput.Text += cust.CompanyName + "\r\n"; } } }

          Sheng

          E R 2 Replies Last reply
          0
          • G gshen

            Ed: The program is as follows: you need a windows form which has a textbox for input, a text box for output, and a button for execute the program. And Also, you need SQL Northwind database to construct a DataContext using Linq to SQL graphical interface. Because the other parts of the windows form and database files are quite messy and confusing (not useful), I just give you the revevant part. Thank you very much! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Data.Linq; using System.Text; using System.Windows.Forms; //using System.Linq.Dynamic; using System.Linq.Expressions; namespace DynamicTry { public partial class DynamicTry : Form { public DynamicTry() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // int i = 5; DynamicDataDataContext ddc = new DynamicDataDataContext(); ParameterExpression pa = Expression.Parameter(typeof(Order), "o"); MemberExpression me = Expression.Property(pa, "OrderID"); ConstantExpression ce = Expression.Constant(10300); Expression ex = Expression.Equal(me, ce); Expression lex = Expression.Lambda<Func<Order, bool>>(ex, new ParameterExpression[] { pa }); IQueryable<Customer> custs = ddc.Customers.Where(c => c.Orders.Where(lex).First() != null); foreach (Customer cust in custs) txtboxOutput.Text += cust.CompanyName + "\r\n"; } } }

            Sheng

            E Offline
            E Offline
            Ed Poore
            wrote on last edited by
            #8

            I don't care about the form stuff, the stuff behind DynamicDataDataContext is the crucial bit.


            I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

            G 1 Reply Last reply
            0
            • E Ed Poore

              I don't care about the form stuff, the stuff behind DynamicDataDataContext is the crucial bit.


              I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

              G Offline
              G Offline
              gshen
              wrote on last edited by
              #9

              Ed: I am not able to send an email with source file attachment to an outside email address. That is a standard SQL Northwind database. I used Linq To SQL graphical designer to add a Linq To SQL class to the project and to drag a Customers table, an Orders table, and an Order_Details table on the designer surface. The datacontext is created automatically. I did not change anything to it. Thanks!

              Sheng

              E 1 Reply Last reply
              0
              • G gshen

                Ed: I am not able to send an email with source file attachment to an outside email address. That is a standard SQL Northwind database. I used Linq To SQL graphical designer to add a Linq To SQL class to the project and to drag a Customers table, an Orders table, and an Order_Details table on the designer surface. The datacontext is created automatically. I did not change anything to it. Thanks!

                Sheng

                E Offline
                E Offline
                Ed Poore
                wrote on last edited by
                #10

                Ok that's all I needed to know, unfortunately at this moment in time I can't spare the time on it (revision for exams on Monday) but hopefully after that I'll be able to have a bit of time. If I need a break over the weekend then I'll take a look into it. Sorry if this causes a problem


                I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

                G 1 Reply Last reply
                0
                • E Ed Poore

                  Ok that's all I needed to know, unfortunately at this moment in time I can't spare the time on it (revision for exams on Monday) but hopefully after that I'll be able to have a bit of time. If I need a break over the weekend then I'll take a look into it. Sorry if this causes a problem


                  I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

                  G Offline
                  G Offline
                  gshen
                  wrote on last edited by
                  #11

                  Ed: Thanks in advance

                  Sheng

                  1 Reply Last reply
                  0
                  • G gshen

                    Ed: The program is as follows: you need a windows form which has a textbox for input, a text box for output, and a button for execute the program. And Also, you need SQL Northwind database to construct a DataContext using Linq to SQL graphical interface. Because the other parts of the windows form and database files are quite messy and confusing (not useful), I just give you the revevant part. Thank you very much! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Data.Linq; using System.Text; using System.Windows.Forms; //using System.Linq.Dynamic; using System.Linq.Expressions; namespace DynamicTry { public partial class DynamicTry : Form { public DynamicTry() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // int i = 5; DynamicDataDataContext ddc = new DynamicDataDataContext(); ParameterExpression pa = Expression.Parameter(typeof(Order), "o"); MemberExpression me = Expression.Property(pa, "OrderID"); ConstantExpression ce = Expression.Constant(10300); Expression ex = Expression.Equal(me, ce); Expression lex = Expression.Lambda<Func<Order, bool>>(ex, new ParameterExpression[] { pa }); IQueryable<Customer> custs = ddc.Customers.Where(c => c.Orders.Where(lex).First() != null); foreach (Customer cust in custs) txtboxOutput.Text += cust.CompanyName + "\r\n"; } } }

                    Sheng

                    R Offline
                    R Offline
                    Roger Alsing 0
                    wrote on last edited by
                    #12

                    I cant test atm since Ive only got vs2005 here, but: Expression lex = Expression.Lambda>(ex, new ParameterExpression[] { pa }); IQueryable custs = ddc.Customers.Where(c => c.Orders.Where(lex).First() != null); The type "Expression" carries no generic type information in that form. thus the "Where(lex)" will fail.. My guess is that you have to use it like: Expression<func<order,bool>> lex = ... You will need the generic type info to make it work..

                    My Blog

                    G 1 Reply Last reply
                    0
                    • R Roger Alsing 0

                      I cant test atm since Ive only got vs2005 here, but: Expression lex = Expression.Lambda>(ex, new ParameterExpression[] { pa }); IQueryable custs = ddc.Customers.Where(c => c.Orders.Where(lex).First() != null); The type "Expression" carries no generic type information in that form. thus the "Where(lex)" will fail.. My guess is that you have to use it like: Expression<func<order,bool>> lex = ... You will need the generic type info to make it work..

                      My Blog

                      G Offline
                      G Offline
                      gshen
                      wrote on last edited by
                      #13

                      Roger: Thank you! Joe Rattz suggested the following solution: http://www.albahari.com/nutshell/linqkit.html This solution is provided by Joe Albahari. It's quite convenient.

                      Sheng

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups