Solved

Cannot convert from IQueryable to ObjectQuery

Posted on 2013-06-13
17
1,196 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now