12

I am using CodeFirst approach and struck with an issue where I need to convert DbSet to ObjectQuery. This is what I did for conversion.

ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext;
ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>();

where db is the context inheriting from DbContext and Request is class.

So, when I try to call the method that expects ObjectQuery as ObjectQueryMethod(objectSet), it throws the following error.

"Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Data.Entity.DbSet<>' and 'System.Data.Objects.ObjectQuery<>'"

Any help is greatly appreciated!

inspiringmyself
  • 590
  • 1
  • 11
  • 29
  • Why would you need to convert a DbSet to an ObjectQuery? – Kittoes0124 Jun 28 '12 at 22:27
  • Provide more details - what is `ObjectQueryMethod`? Your current approach is correct. You cannot convert `DbSet` to `ObjectQuery`. You must create `ObjectSet` and use it instead of `DbSet`. – Ladislav Mrnka Jun 29 '12 at 08:23
  • @Kittoes: I am trying to implement Advanced searching in JQGrid and ObjectQuery seems to be the right one to parse the parameters (filters) sent back to the server. – inspiringmyself Jun 29 '12 at 13:23
  • @LadislavMrnka: It's just a method that expects ObjectQuery as a parameter (named as such) :) – inspiringmyself Jun 29 '12 at 13:27
  • Thanks everyone for your time. I figured out what I was missing, I need to pass the DbSet("Requests") to ObjectSet as ObjectSet objectSet = objectContext.CreateObjectSet("Requests"); – inspiringmyself Jun 29 '12 at 13:32

2 Answers2

23

I found the answer. Of course, it is possible to convert DbSet in Entity framework to ObjectQuery using the below lines of code.

ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext;  
ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>("Requests");

where,

  • db - Context class inherting from DbContext.
  • Requests - DbSet<Request> defined in Context class.
  • objectSet - Can now be passed as ObjectQuery.
Duncan Smart
  • 31,172
  • 10
  • 68
  • 70
inspiringmyself
  • 590
  • 1
  • 11
  • 29
2

Thanks for the correct answer 'inspiringmyself'; this is just to elaborate on your answer. I managed to do this with a generic type, so just to share it:

private List<T> GetByCustomCriteria<T>(string criteria) where T: class
{
  var objectContext = ((IObjectContextAdapter)_myModelEntities).ObjectContext;
  //note: _myModelEntities is a DbContext in EF6.
  var objectSet = objectContext.CreateObjectSet<T>();
  return new List<T>(objectSet.Where(criteria));
 }

And I thought the above post could do with an example of what criteria to send so here's an example:

 //sample criteria for int field:
 var myClientById = GetByCustomCriteria<Client>("it.Id == 1");`

//sample criteria for string field, note the single quotes
var myClientByName = GetByCustomCriteria<Client>("it.Surname == 'Simpson'"); 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Spyder
  • 3,784
  • 3
  • 26
  • 15