none
Entry Framework 4.5: Query Builder RRS feed

  • Question

  • I'm a newby to Entry Framework. I'm working though Julia Lerman's book and I'm stuck with Query Builder method. I get the following compile error:

     Error   1   'System.Data.Entity.DbSet<Chapter2ConsoleApp.Contact>' does not
    contain a definition
    for 'Where' and the best extension method overload
    'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>,
    System.Linq.Expressions.Expression<System.Func<TSource,bool>>)'
    has some invalid arguments 
    C
    :\EF\ProgrammingEntityFramework\Chapter2ConsoleApp\Program.cs  59  32 
    Chapter2ConsoleApp

    There's the code.

    using (SampleEntities context = new SampleEntities())
                {
                  var contacts = context.Contacts.Where("it.FirstName = 'Robert'").OrderBy("it.LastName");
                                    foreach (Contact contact in contacts)
                    {
                        Console.WriteLine("{0} {1}",
                                    contact.FirstName.Trim(),
                                    contact.LastName);
                    }
                }
                Console.Write("Press Enter...");
                Console.ReadLine();
            }

    A Linq to entity with a lambda works OK. When I look at the bad code via IntelliSense I can see that there is a Where method. Finally, I've tried a cast [(IObjectContextAdapter)] on the context but then I couldn't get the Contact method.

    Would anyone help me?

    Saturday, March 30, 2013 4:30 PM

Answers

  • Hi JohnBrm;

    From Microsoft documentation, "ObjectQuery also implements a set of query builder methods that can be used to sequentially construct query commands that are equivalent to Entity SQL".

    The ObjectQuery class states, "Represents a typed query against a conceptual model in a given object context.", note that it states an object context and NOT a DbContext. Therefore to get the code to work you either work with a ObjectContext which is NOT the default when creating a EF model with version 4.1 and above or you use the IObjectContextAdapter of the DbContext class to get an instance of the ObjectContext from the current DbContext.

    // Using the DbContext: The following code should work.
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;
    
    // Add the following line to get a ObjectContext
    ObjectContext objCtx = ((IObjectContextAdapter) context).ObjectContext;
    
    // Modify the following line to execute the query
    var contacts = objCtx.CreateObjectSet<Contact>().Where("it.FirstName = 'Robert'")
                          .OrderBy("it.LastName");

    If you switch to using ObjectContext then there is no change to your code.  

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Chester HongModerator Monday, April 1, 2013 5:54 AM
    • Marked as answer by JohnBrm Monday, April 1, 2013 9:21 AM
    • Unmarked as answer by JohnBrm Monday, April 1, 2013 9:26 AM
    • Unproposed as answer by JohnBrm Monday, April 1, 2013 9:27 AM
    • Marked as answer by JohnBrm Monday, April 1, 2013 9:28 AM
    Sunday, March 31, 2013 5:34 AM

All replies

  •   If I were using lambada with Linq, it would be like below.
     var contacts = context.Contacts.Where(a => a.FirstName = 'Robert').OrderBy(a => a.LastName);
          foreach (Contact contact in contacts)
     Are you sure you are doing it right.

    http://linqtextquerybuilder.codeplex.com/

    Saturday, March 30, 2013 6:49 PM
  • Hi JohnBrm;

    From Microsoft documentation, "ObjectQuery also implements a set of query builder methods that can be used to sequentially construct query commands that are equivalent to Entity SQL".

    The ObjectQuery class states, "Represents a typed query against a conceptual model in a given object context.", note that it states an object context and NOT a DbContext. Therefore to get the code to work you either work with a ObjectContext which is NOT the default when creating a EF model with version 4.1 and above or you use the IObjectContextAdapter of the DbContext class to get an instance of the ObjectContext from the current DbContext.

    // Using the DbContext: The following code should work.
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;
    
    // Add the following line to get a ObjectContext
    ObjectContext objCtx = ((IObjectContextAdapter) context).ObjectContext;
    
    // Modify the following line to execute the query
    var contacts = objCtx.CreateObjectSet<Contact>().Where("it.FirstName = 'Robert'")
                          .OrderBy("it.LastName");

    If you switch to using ObjectContext then there is no change to your code.  

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Chester HongModerator Monday, April 1, 2013 5:54 AM
    • Marked as answer by JohnBrm Monday, April 1, 2013 9:21 AM
    • Unmarked as answer by JohnBrm Monday, April 1, 2013 9:26 AM
    • Unproposed as answer by JohnBrm Monday, April 1, 2013 9:27 AM
    • Marked as answer by JohnBrm Monday, April 1, 2013 9:28 AM
    Sunday, March 31, 2013 5:34 AM
  • Hi darnold924.

    I had early created a Linq To Entities call with no problem. As I'm starting with EF I won't to look in to Query Builder methods.

    Once again, many thanks for your help.

    Sunday, March 31, 2013 7:43 AM
  • Hi Fernando,

    Thank you very much for you solution!

    I found one of your replies to someones question (in the same area) an I got as far as IObjectContectAdapter in this call: With your example I think I know unterstand how I can use ObjectContext.... time will see.

    ObjectQuery<Contact> contacts = ((IObjectContextAdapter) context).ObjectContext.CreateQuery<Contact>(queryString);


    Many Thanks.
    • Edited by JohnBrm Sunday, March 31, 2013 7:58 AM
    • Marked as answer by JohnBrm Sunday, March 31, 2013 7:58 AM
    • Unmarked as answer by JohnBrm Monday, April 1, 2013 9:26 AM
    Sunday, March 31, 2013 7:57 AM
  • Hi JohnBrm;

    Did you assign the points to the wrong post? Your solution is what I posted but you combined both statements into 1 where I posted it in 2 separate statements for clarity. Also in what I posted ObjectContext can be used without creating a new one seeming I keep a reference to it.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, March 31, 2013 1:58 PM
  • Hi Fernando,

    Sorry for the confusion. I am new to MSDN and when I copied my code it was in the wrong place. It should have be in sentance 3 (... in this call:).

    Your example was very clear. Thank you. I was trying to say that I have used IObjectContextAdapter before but not correctly. Thanks to to your help I now understand it.

    Was again, many thanks for your help.

    John

    Sunday, March 31, 2013 8:05 PM
  • Hi John;

    My comment was that you did not mark my post as the answer to the question but marked your post as the answer to your own question.

       


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, March 31, 2013 8:34 PM
  • Once again,

    thanks for your help.

    John

    Monday, April 1, 2013 9:22 AM
  •   

    Not a problem John, glad I was able to help.

    Thank you for updating the solution. Have a nice day.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, April 1, 2013 1:11 PM