Solved

Cannot convert from IQueryable to ObjectQuery

Posted on 2013-06-13
17
1,345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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