简体   繁体   中英

How to define anonymous method types to build dynamic queries with LINQ?

I'm busy with a LINQ to SQL project that basically creates multiple threads for each entity type in my database, which constantly queries information from the DB in a thread.

Here's a pseudo example:

streamer.DefineDataExpression<Contacts>(x => x.FirstName == "Bob");
while(true)
{
     List<Contacts> MyContactsResult = streamer.ResultList;
     // do whatever with MyContactsResult
}

The above code doesn't exist, but this is what I have so far for the 'streamer' class (it obviously doesn't work, but you can see what I'm trying to achieve above):

public void DefineExpression(System.Linq.Expressions.Expression<System.Func<T, bool>> expression)
{
    using (var db = new LINQDataContext())
    {
        ResultList = db.GetTable<T>().Where(expression);
    }
}

How do I go about creating a method like 'DefineExpression' that will allow me to query a LINQ type dynamically?

Why not use the Dynamic LINQ provider, as mentioned by Scott Guthrie . I think that would give you everything you are looking for, because you can define the query as a string. Therefore, you can more easily build a string representation of your query, and execute on the fly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM