Passing LINQ expressions as parameters

Brian Wirt

I have an Entity Framework class set up to read a table from a SQL database, but I can't quite figure out how to pass a LINQ expression to filter only certain objects. I know there is a way to build an expression tree and do this dynamically from within the class, but I can't seem to figure the best way to do this.

Any tips are appreciated.

class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class MyObjectCollection<T> where T : class
{
    private List<T> myInternalCollection = new List<T>();

    MyObjectCollection()
    {
        using (var db = new MyContext()) 
        {
            foreach (T row in db.Set<T>())
            {
                // Enumerate the data, do whatever with it...
                myInternalCollection.Add(row);
            }
        }
    }

    MyObjectCollection(var MyLinqExpression)
    {
        using (var db = new MyContext()) 
        {
            foreach (T row in db.Set<T>().Where.MyLinqExpression()
            {
                // Enumerate the data, do whatever with it...
                myInternalCollection.Add(row);
            }
        }
    }
}

// Works fine:
MyObjectCollection<Customer> allCustomers = new MyObjectCollection<Customer>();

// Would like something like this:
MyObjectCollection<Customer> customersP = new MyObjectCollection<Customer>(c => c.StartsWith("P"));
Russell Jonakin

The Linq Where method takes a parameter of Func<T, bool> where T would be the dbSet object type you want to apply your Where method on.

So to make your code work, you can do this:

public MyObjectCollection(Func<T, bool> MyLinqExpression)
{
    using (var db = new MyContext())
    {
        foreach (T row in db.Set<T>().Where(MyLinqExpression))
        {
            // Enumerate the data, do whatever with it...
            myInternalCollection.Add(row);
        }
    }
}

UPDATE: Also, to achieve the functionality you are looking for with a generic collection, instead of encapsulating a private List object, you could inherit from List like I show below. So, if you wanted MyObjectCollection to function like a List, you can do something like I show below.

So with your code from above, you can change to this:

public class MyObjectCollection<T> : List<T> where T : class
{
    public MyObjectCollection()
    {
        using (var db = new MyContext())
        {
            foreach (T row in db.Set<T>())
            {
                // Enumerate the data, do whatever with it...
                this.Add(row);
            }
        }
    }

    public MyObjectCollection(Func<T, bool> MyLinqExpression)
    {
        using (var db = new MyContext())
        {
            foreach (T row in db.Set<T>().Where(MyLinqExpression))
            {
                // Enumerate the data, do whatever with it...
                this.Add(row);
            }
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Extending LINQ Expressions

分類Dev

Passing parameters to middleware in Laravel

分類Dev

Passing parameters to a custom URI

分類Dev

Passing parameters to AngularJS $timeout

分類Dev

Passing parameters to a custom URI

分類Dev

Passing parameters to jest function

分類Dev

Passing parameters to a OPENQUERY

分類Dev

passing BlocProvider with class parameters

分類Dev

Passing parameters to a react component

分類Dev

Error with passing parameters

分類Dev

Maple passing parameters by reference

分類Dev

Iframe not passing url parameters

分類Dev

Linq Dynamic Expressions Groupby with sum

分類Dev

Golang: Passing structs as parameters of a function

分類Dev

Passing additional parameters to MapStruct mapper

分類Dev

Passing parameters in rmarkdown to the text or headings

分類Dev

Passing parameters to scalameta paradise macro

分類Dev

Angular 6 passing in parameters into URL

分類Dev

Passing parameters in java script function ?

分類Dev

What is the difference between these two LINQ expressions?

分類Dev

Supporting "out / ref" parameters in expressions with conversion to "object"

分類Dev

The WHERE IN clause with two parameters in linq

分類Dev

Passing list of enum values as HTTP query parameters

分類Dev

Passing parameters through Ajax success function

分類Dev

Passing list of enum values as HTTP query parameters

分類Dev

Unpacking a dictionary and passing to a function as keyword parameters

分類Dev

Passing -e and -n as positional parameters in Bash

分類Dev

Is there a way to call function without passing its parameters?

分類Dev

Creating multiple objects and passing parameters through constructor

Related 関連記事

ホットタグ

アーカイブ