score:1
Well, I don't know if you can cut it down to one pass, but you can get information on the member, and if it coincides with another variable you have declared as being part of the query (in this case "o"), you use it to generate your query.
Otherwise, you would assume it is a constant, and then plug that value in.
Unfortunately, because you can have from statements (in addition to the let statement) in multiple places in the query, it doesn't seem like you can do it in just one pass, since you need to know all of the query variables up front.
score:1
Okay, after some quick statistical analysis (ie, comparing individual properties manually), DeclaringType, ReflectedType and Namespace are when Lambda parameter is not in scope" fires.
So unless someone comes up with a better answer, that might be all I have to go on.
score:1
In a Where expression, you are looking at an Expression<Func<T,bool>>
- which means that the outermost lambda should have a single ParameterExpression
with type T
.
If a comparison relates to the row, it will have (as an ancestor) this ParameterExpression
; if it is local variable, it will have (as an ancestor) a ConstantExpression
- however, the type of this constant expression will be compiler generated to cope with all of the captured variables used in the expression.
Like so:
using System;
using System.Linq.Expressions;
class Foo
{
public string Name { get; set; }
static void Main()
{
var exp = (LambdaExpression) GetExpression();
WalkTree(0, exp.Body, exp.Parameters[0]);
}
static void WriteLine(int offset, string message)
{
Console.WriteLine(new string('>',offset) + message);
}
static void WalkTree(int offset, Expression current,
ParameterExpression param)
{
WriteLine(offset, "Node: " + current.NodeType.ToString());
switch (current.NodeType)
{
case ExpressionType.Constant:
WriteLine(offset, "Constant (non-db)"
+ current.Type.FullName);
break;
case ExpressionType.Parameter:
if (!ReferenceEquals(param, current))
{
throw new InvalidOperationException(
"Unexpected parameter: " + param.Name);
}
WriteLine(offset, "db row: " + param.Name);
break;
case ExpressionType.Equal:
BinaryExpression be = (BinaryExpression)current;
WriteLine(offset, "Left:");
WalkTree(offset + 1, be.Left, param);
WriteLine(offset, "Right:");
WalkTree(offset + 1, be.Right, param);
break;
case ExpressionType.MemberAccess:
MemberExpression me = (MemberExpression)current;
WriteLine(offset, "Member: " + me.Member.Name);
WalkTree(offset + 1, me.Expression, param);
break;
default:
throw new NotSupportedException(
current.NodeType.ToString());
}
}
static Expression<Func<Foo, bool>> GetExpression()
{
Foo foo = new Foo { Name = "abc" };
return row => row.Name == foo.Name;
}
}
Source: stackoverflow.com
Related Articles
- Determining scope of a MemberExpressions target
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- Cannot convert source type to target type List<KeyValuePair> Linq
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Cannot convert source type system.nullable to target type int
- creating Linq to sqlite dbml from DbLinq source code
- Dynamicaly changing the type of a parameter in an Expression without knowing the target type at code time
- source code for LINQ 101 samples
- Linq Scope Problem + Reduce Repeated Code
- Cannot convert source type 'System Linq IQueryable<decimal>' to target type decimal
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Determining the scope of LINQ statement with a using around the DB context
- Find a certain value from a source collection in a target collection of different type with linq
- IEnumerable.Except() on LINQ doesn't run between ideal source and target lists
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- Mutating the expression tree of a predicate to target another type
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- Using Dynamic Linq to Filter entity by datetimeoffset columns
- Make JOIN query match even if row being joined does not exist
- Check if collection contains element with specific properties
- Enforce Entity Framework to use datetime than datetime2
- LINQ - How to write a query to set a variable bool True or False
- How do you have 2 where clauses in a linq xml statement
- Advanced search with LINQ to EF
- Linq - Except one list with items in another
- create XML file for each record of the DataTable using Linq C#
- Linq: Join 2 tables that share one column name (with different data)
- Get nested properties out in a list - using Index
- LINQ to table by foreign key in another table
- NHibernate nullability check inside where does not work
- Creating OrderBy parameter dynamically
- linq one liner to remove elements from IDictionary whose keys are not in another Dictionary
- I need help re-writing a SQL query into LINQ format
- C# Linq - Select Multiple Fields where one field should also be filtered with a select
- read xml file having namespace in root
- Linq to SQL DataContext fails to update Enum field in database for inherited objects
- The function 'CreateTime' is not supported by SQL Server Compact