Solved

Cannot convert from IQueryable to ObjectQuery

Posted on 2013-06-13
17
1,286 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

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 …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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