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 Query
- Get list of properties in LINQ projection
- Linq query with select needed to get specific properties from a list
- Linq code to get the index of an object in an array from an object within a list
- How to sort list on multiple properties in one line of code in Linq
- I am using LINQ to connect to a database and i get a list of properties and I want to convert it to an XML File
- get specific properties within the generic list using linq
- c# Linq or code to extract groups from a single list of source data
- LINQ Get full list of objects, include only the properties that match certain condition
- How do I use LINQ to obtain a unique list of properties from a list of objects?
- Get index of object in a list using Linq
- Get indexes of all matching values from list using Linq
- How to get distinct instance from a list by Lambda or LINQ
- Using LINQ to find item in a List but get "Value cannot be null. Parameter name: source"
- Linq way to get piecewise difference between element and next element in list
- LINQ way to get items between two indexes in a List
- LINQ - Get all items in a List within a List?
- Is it possible to use Linq to get a total count of items in a list of lists?
- LINQ Get Distinct values and fill LIST
- get last in LINQ list
- How to get a list of the grouped values in linq groupby?
- Get all pairs in a list using LINQ
- How to get linq `ForEach` statement to return data on the method call being made for each list object?
- How do I get list of id's as int using LINQ
- Linq query to get the distinct values in a list
- LINQ with subselect and groupby to get only the latest version of each item in a list
- Using LINQ to Get Sum/ Average of a List with custom objects
- How to get all elements except the n'th element in a List using Linq
- Group items in a list based on two properties using LINQ
- LINQ - get total count of values from nested list
- Get first item in list from linq query
More Query from same tag
- Disjoint Union in LINQ
- Why is my function to filter files by User ID not filtering any files?
- LINQ C# Sum and changing value to time (hh:mm:ss) in datatable
- how to filter list using linq windows phone ListPicker
- LINQ lambda expression for many-to-one inner join involving max()
- Excluding result based on length of a field
- How to get current month in Query
- Entityframework error: "The type '......' appears in two structurally incompatible initializations within a single LINQ to Entities query
- How to use multiple 'Where' expressions and chain them together with AND and OR using C#/.NET?
- entity framework - inner join to left join
- LINQ to PostgreSQL errors when using DbLINQ
- Linq to XML if/foreach with XElement
- Kendo Paging with Linq to SQL Classes, Query All Data Very Slow
- Linq Except returning entire first set with custom comparer
- Databinding between XML file and GUI
- How do I OrderBy a DateTime difference in Linq?
- How do you create a nested LINQ Grouping / Lookups for a Collection of Objects
- Linq - Take x-amount of distinct items from a list
- int' does not contain a definition for 'contains' and no extension method 'contains' accepting a first argument of type 'int' could be found
- Intersecting any number of Lists
- XDocument C# Join
- Creating hierarchical JSON result from multiple tables using linq expression
- Is there a nicer way to write this linq query?
- The order of elements vary in an IQueryable, every time I query with the same condition
- Load related nested Entities of included List
- Get the next object in list after the object with current ID
- Implementing LINQ-like functionality in Scala
- Linq statement to iterate through a collection and making sure each item is in the correct order?
- LINQ on a LinkedList - iterate over LinkedListNode<T>, not T
- ASP.NET MVC 3 multiple Models to single Form using DB