?
Solved

Entity Framework Query

Posted on 2011-05-02
3
Medium Priority
?
236 Views
Last Modified: 2012-08-13
I am using the code to return a query.  This works just fine but I only want to return the Outputs With a particular ProjectID.  ProjectID being an field on the Outputs table.  How do I add this to the query?


The Structure is as below.
CoreAims
   Objectives
         Outputs
            (projectId)
Public Function GetAllObjectives1() As IQueryable(Of CoreAim)

        Dim Objectives = (From O In Me.ObjectContext.CoreAims.Include("Objectives").Include("Objectives.Outputs")
                           Select O)
        Return Objectives

    End Function

Open in new window

0
Comment
Question by:Kevin Robinson
  • 2
3 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35505463
Please try the following.  It uses a different LINQ syntax, but it enables checking the ProjectId.

Public Function GetAllObjectives1() As IQueryable(Of CoreAim)

	Dim Objectives = From aim In Me.ObjectContext.CoreAims _
	                 From objective In aim.Objectives _
			 From output In objective.Outputs _
			 Where output.ProjectId = 1 _ ' <<< Your project Id
			 Select output
	Return Objectives

End Function

Open in new window

0
 
LVL 3

Author Comment

by:Kevin Robinson
ID: 35511439
I am getting this error

System.ServiceModel.DomainServices.Client.DomainOperationException: Load operation failed for query 'GetAllObjectives1'. Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[OperationalPlanning.Output]' to type 'System.Linq.IQueryable`1[OperationalPlanning.CoreAim]'.
 
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35513239
Sorry, it should be Select aim not Select output.  I also included the Includes you had initially to avoid any lazy loading. There you go:

Public Function GetAllObjectives1() As IQueryable(Of CoreAim)

	Dim Objectives = From aim In Me.ObjectContext.CoreAims.Include("Objectives").Include("Objectives.Outputs") _
	                 From objective In aim.Objectives _
			 From output In objective.Outputs _
			 Where output.ProjectId = 1 _ ' <<< Your project Id
			 Select aim

	Return Objectives

End Function

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
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!
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 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