Search code examples
linqserializationentity-framework-4expression-trees

Save Entity Framework Linq Query to database


I was wondering if we can convert a Linq Query on the Entity Framework and save the query to the database by converting it to an Expression Tree and Serializing. Can someone please help me on this and point me in a right direction whether this can be done or not. Any help is greatly appreciated on this.

Thanks, Ajay.


Solution

  • i released a library for that purpose just yesterday. Serialize.Linq. It serializes linq expressions to xml, json or binary.

    using System.Linq.Expressions
    using Serialize.Linq.Extensions;
    
    Expression<Func<Person, bool>> query = p => p.LastName == "Miller" 
        && p.FirstName.StartsWith("M");
    
    Console.WriteLine(query.ToJson());
    Console.WriteLine(query.ToXml());