Solved

Cannot convert from IQueryable to ObjectQuery

Posted on 2013-06-13
17
1,252 Views
Last Modified: 2013-06-24
Hi guys,

I am getting the above error (Cannot convert from System.Linq.IQueryable to System.Data.Objects.ObjectQuery)

My auto generated code is...

 System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = bOM_Entities.BomDataUploads;
return bomDataUploadsQuery;

Open in new window


I was trying to add a where clause. So used..

return bomDataUploadsQuery.Where(c=>c.Manufacturer.Contains("Random Text String"));

Open in new window


Why am I getting this error and what is the solution?

Many Thanks,
Dean
0
Comment
Question by:deanlee17
  • 9
  • 8
17 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244113
ObjectQuery<T> inherits IQueryable<T> so why do u want to return resuts as ObjectQuery?
0
 

Author Comment

by:deanlee17
ID: 39244195
I was just working with the auto generated code (slightly modified)....

System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = bOM_Entities.BomDataUploads;

return bomDataUploadsQuery;

 context = new Ascent_ERP.BOM_Entities();
            // Load data into BomDataUploads. You can modify this code as needed.
            System.Windows.Data.CollectionViewSource bomDataUploadsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("bomDataUploadsViewSource")));
            System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = this.GetBomDataUploadsQuery(context);
            bomDataUploadsViewSource.Source = bomDataUploadsQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39252511
anybody?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 42

Expert Comment

by:sedgwick
ID: 39252517
can u post the whole function?
0
 

Author Comment

by:deanlee17
ID: 39252645
private void LoadBomRetrieve()
        {
            context = new Ascent_ERP.BOM_Entities();
            // Load data into BomDataUploads. You can modify this code as needed.
            System.Windows.Data.CollectionViewSource bomDataUploadsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("bomDataUploadsViewSource")));
            System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = this.GetBomDataUploadsQuery(context);
            bomDataUploadsViewSource.Source = bomDataUploadsQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
        }


  private System.Data.Objects.ObjectQuery<BomDataUpload> GetBomDataUploadsQuery(BOM_Entities bOM_Entities)
        {

		System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = bOM_Entities.BomDataUploads;
                return bomDataUploadsQuery;
        }

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252651
which line triggers the conversion error?
0
 

Author Comment

by:deanlee17
ID: 39252655
Ive given you the original code, I want a WHERE clause on bomDataUploadsQuery (that gets a value from a combobox). Forget the code I was trying to use before. Start fresh.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252670
change GetBomDataUploadsQuery to:

private IQueryable<BomDataUpload> GetBomDataUploadsQuery(BOM_Entities bOM_Entities)
        {

		System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = bOM_Entities.BomDataUploads;
                return bomDataUploadsQuery.Where(c=>c.Manufacturer.Contains("Random Text String"));;
        }

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39252738
Cannot implicitly convert type 'System.Linq.IQueryable<Ascent_ERP.BomDataUpload>' to 'System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload>'. An explicit conversion exists (are you missing a cast?)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252775
private IQueryable<BomDataUpload> GetBomDataUploadsQuery(BOM_Entities bOM_Entities)
        {

		System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = bOM_Entities.BomDataUploads;
                return (IQueryable<BomDataUpload>)bomDataUploadsQuery.Where(c=>c.Manufacturer.Contains("Random Text String"));;
        }

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39252958
Attached
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39253072
attached what?
0
 

Author Comment

by:deanlee17
ID: 39253089
Sorry my mistake....
error.png
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39253098
u didn't change the function declaration return type:
private IQueryable<BomDataUpload> GetBomDataUploadsQuery(BOM_Entities bOM_Entities)
        {

		System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload> bomDataUploadsQuery = bOM_Entities.BomDataUploads;
                return bomDataUploadsQuery.Where(c=>c.Manufacturer.Contains("Random Text String"));;
        }

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39253117
Think ive got it...

return (System.Data.Objects.ObjectQuery<Ascent_ERP.BomDataUpload>)bomDataUploadsQuery.Where(c => c.Manufacturer.Contains("66"));

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39253121
AH my mistake!!

Perfect thanks
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39253131
u welcome :-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question