score:1
You can easily do that using an ExpressionVisitor
. Just create a new class and override the visiting methods. If you know that the projection was done using member bindings, you can simply override the method VisitMemberBinding
and add the bound member to a list that you store as an instance variable. Then all you need to do is to make that instance variable public.
class ProjectionAnalyzer : ExpressionVisitor
{
private HashSet<MemberInfo> boundMembers = new HashSet<MemberInfo>();
protected override MemberBinding VisitMemberBinding(MemberBinding node)
{
boundMembers.Add(node.Member);
return base.VisitMemberBinding(node);
}
public IEnumerable<MemberInfo> BoundMembers => boundMembers;
}
Then, use this class as follows:
var analyzer = new ProjectionAnalyzer();
analyzer.Visit(selectorPredicate);
var boundMembers = analyzer.BoundMembers;
How you obtain the selector predicate depends on your LINQ provider.
score:1
I did something similar using VisitMemberAssignment:
namespace BoundPropertiesinQuery
{
static class IEnumerableExtensions
{
class ProjectedVisitor : ExpressionVisitor
{
public IList<string> ProjectedPropertyNames { get; set; } = new List<string>();
protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
{
ProjectedPropertyNames.Add(node.Member.Name);
return base.VisitMemberAssignment(node);
}
}
public static IEnumerable<string> ProjectedProperties<T>(this IQueryable<T> @this)
{
var pv = new ProjectedVisitor();
pv.Visit(@this.Expression);
return pv.ProjectedPropertyNames.Distinct();
}
}
internal class MyObject
{
public int Property1 { get; set; }
public int Property2 { get; set; }
public int Property3 { get; set; }
public int Property4 { get; set; }
}
internal class MyOtherObject
{
public int other1 { get; set; }
public int other2 { get; set; }
public int other3 { get; set; }
public int other4 { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
var listOfItems = new List<MyOtherObject>()
{
new MyOtherObject
{
other1 = 1,
other2 = 2,
other3 = 3,
other4 = 4
},
new MyOtherObject
{
other1 = 5,
other2 = 6,
other3 = 7,
other4 = 8
}
};
var result = listOfItems.AsQueryable().Select(m => new MyObject
{
Property1 = m.other1,
Property2 = m.other2
}).ProjectedProperties();
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Source: stackoverflow.com
Related Articles
- Get list of properties in LINQ projection
- How to sort list on multiple properties in one line of code in Linq
- c# Linq or code to extract groups from a single list of source data
- How do I use LINQ to obtain a unique list of properties from a list of objects?
- Group items in a list based on two properties using LINQ
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- LINQ Source Code Available
- LINQ query to detect duplicate properties in a list of objects
- Combine object properties into a list with LINQ
- C# : LINQ query to list all empty properties of a class
- sorting list of objects with null properties using linq
- LINQ query returns old results when source list is re-initialized
- Update all properties in list using linq
- C# - Linq optimize code with List and Where clause
- LINQ filter list based on property value and filter the properties as well
- Linq Select list of dynamic properties into another Object?
- creating Linq to sqlite dbml from DbLinq source code
- Clean some properties value in List with Linq based on a value of the list
- Linq - Sort list of objects by several contained properties
- Combine Two Properties From Entity In List And Flatten It With Linq
- Use linq to make a projection of an anonymous class with a list
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- Linq projection that flattens a list into a deliminated string
- Linq query with select needed to get specific properties from a list
- LINQ - Grouping a list by multiple properties and returning an object with an array member
- Performing a LINQ search on all properties of a list of entities without duplicates
- Create a entity framework LINQ group join query to return a subset of properties from DTO and a list of another DTO, ASP.net core
- Searching if all properties exist in a list of entities using Linq
- Code practice to handle empty result set in Linq to custom list
- c# - Linq select properties from List of objects and modify them with shorthand conditional
- Fill items List in List from another List
- best way to select...where in using linq
- Build an lambda Expression tree with a specific field in a linked entity
- Is there a way to turn this small method into a lambda or linq statement?
- MemberExpression to MemberExpression[]
- Search DataTable using LINQ
- Strongly Typing a LINQ Query using multiple keys of complex objects
- Is it possible to change & to & in the results from a LINQ query?
- Getting averages from different table in Entity Framework
- Read and Remove invalid characters from xml outside xml elements in C# Linq to Xml
- c# - Extract first integer from a string
- how to break the loop if condition satisfies in linq?
- How to handle no matches case in List.First in c#?
- Copy an xml section
- Retrieve Parent / Child Query result SQL Server with c#
- Include null cells in Linq query from DB in C# code
- How to override the Linq select statement?
- transform a lambda expression
- Get IDictionary(of String, IDictionary(of String, String)) using Linq
- Select an element who as a last child with property