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 Query
- 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?
More Query from same tag
- c# Group a list and get the count of each group
- How to lookup entities in EntityFramework 4 by a finder interface? (works in LinqToSql)
- How to replace FirstOrDefault with something like RandomOrDefault to "balance" calls?
- linq join question
- Linq data base insertion error
- How to fill dataset from LINQ
- LINQ query for the following
- How to build LINQ-query dynamically based on array of input arguments
- LINQ GroupBy result of previous GroupBy
- Incorporate if clause into linq
- Await for IEnumerable items, (wait after await)
- C# and displaying a row randomly for a sight word game
- C#/LINQ: Concatenating strings
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Cannot initialize type 'class' with a collection initializer because it does not implement 'IEnumerable'
- Summing a column gives null in C#/ASP.NET
- Search for an Array or List in a List
- Linq query get sum from list where certain property equals to
- How can I count total item in an object/record and count only the value = true
- Convert ?: statement to handle more than 2 answers
- Does a code that combines single() with yield make any sense?
- With LINQ, why does a GroupBy when there's only one record not return any rows?
- EF Core 2.1, force SelectMany to produce LEFT JOIN
- How to convert rows to columns in LINQ
- Does LINQ cache computed values?
- Split array into array of arrays
- LINQ to SQL does not update when data has changed in database
- Covert SQL to Linq Query
- Read XML node from certain element forward?
- How compare 2 string arrays with duplicate records