Dynamic Query and Lambda expression construction [modified]
-
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
-
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
-
Can you post it with the necessary <T> brackets? It doesn't really make anysense without them.
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
-
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
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
-
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
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
-
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
-
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
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
-
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
-
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
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
-
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
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
-
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
-
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
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..
-
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..
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