-1

Can anybody explain me how to use (1) iQueryable (2) Expression Tree in C# by providing a very basic example? Both are not correlated, instead of making two separate questions, I wish to clear my doubt in a single question.

Advanced Thanks.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
user184805
  • 1,839
  • 4
  • 19
  • 16

3 Answers3

3

Expression trees are very simple to make:

Expression<Func<int,int,int>> addExp = (a,b) => a + b;

or

var paramA = Expression.Parameter(typeof(int), "a");
var paramB = Expression.Parameter(typeof(int), "b");
Expression<Func<int,int,int>> addExp = Expression.Lambda<Func<int,int,int>>(
    Expression.Add(paramA, paramB),
    paramA,
    paramB);

Building an IQueryable provider is fairly difficult. However, Matt Warren has a very indepth series that walks you through creating an IQueryable provider.

user7116
  • 63,008
  • 17
  • 141
  • 172
  • Fresh link to Matt's series: https://blogs.msdn.microsoft.com/mattwar/2008/11/18/linq-building-an-iqueryable-provider-series/ – rnort Dec 24 '19 at 12:12
2

I generally don't like just linking stuff, but this is a more complicated topic. I suggest watching this video:

http://channel9.msdn.com/shows/Going+Deep/Erik-Meijer-and-Bart-De-Smet-LINQ-to-Anything/

Erik does a great job of explaining this, and gives a neat Linq to Simpsons example.

BFree
  • 102,548
  • 21
  • 159
  • 201
0
Expression<Func<T, string, PropertyInfo>> expression = (obj, str) => 
    obj.GetType()
       .GetProperty(
           obj.GetType()
              .GetProperties()
              .ToList()
              .Find(prop =>
                    prop.Equals(str, StringComparison.OrdinalIgnoreCase).Name.ToString());
var obj = expression.Compile()(rowsData.FirstOrDefault(), sortIndex);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
suneelsarraf
  • 923
  • 9
  • 7