Solved

Cannot convert from IQueryable to ObjectQuery

Posted on 2013-06-13
17
1,223 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

15 Experts available now in Live!

Get 1:1 Help Now