Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6563715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:52:55+00:00 2026-05-25T13:52:55+00:00

How can I return return a collection in a method from a LINQ query

  • 0

How can I return return a collection in a method from a LINQ query that has a one to many relationship?

For instance, I have the following code where I can have many Projects to a TimeTracking object. Will the type that I have defined for the return type (IEnumerable) work? It is set up in my EF model as this specific kind of relationship.

public IEnumerable<TimeTracking> GetTimeTrackings()
        {
            YeagerTechEntities DbContext = new YeagerTechEntities();

            DbContext.Configuration.ProxyCreationEnabled = false;
            DbContext.Configuration.LazyLoadingEnabled = false;

            var timeTrackings = (from timeTrackingProjects in DbContext.TimeTrackings.Include("TimeTrackings.Projects")
                                 select timeTrackingProjects).Where(p => p.TimeTrackingID > 0);

            CloseConnection(DbContext);

            return timeTrackings;
        }

If so, when I display it in my MVC 3 View, and my View contains an IEnumerable<YeagerTech.YeagerTechWcfService.TimeTracking> model, will the model variable have records in it for the TimeTracking and Project objects? I don’t think it will. My TimeTracking object is set up as follows unless I need to inherit the Project class with it (which would then have the Project properties):

    public partial class TimeTracking
        {
            [DataMember]
            public int TimeTrackingID { get; set; }

            [DataMember]
            public short ProjectID { get; set; }

            [DataMember]
            public byte[] Attachment { get; set; }

            [Required]
            [DataType(DataType.Date)]
            [DataMember]
            public System.DateTime StartDate { get; set; }

            [Required]
            [DataType(DataType.Date)]
            [DataMember]
            public System.DateTime EndDate { get; set; }

            [DataMember]
            public string Notes { get; set; }

            [DataMember]
            public System.DateTime CreatedDate { get; set; }

            [DataMember]
            public Nullable<System.DateTime> UpdatedDate { get; set; }

            [DataMember]    
            public virtual Project Project { get; set; }
        }

I also want my View to display the Project text that is associated with the TimeTracking and not the Project value. How can I do this?Can someone please help?

I got the following msg from invoking a method on my WCF client.

‘cannot be serialized if reference tracking is disabled’

After getting the msg, I then modified my DataContracts to include references ([DataContract(IsReference = true)]).

namespace YeagerTechModel
{
    [Serializable]
    [DataContract(IsReference = true)]
    public partial class Customer
    {
        public Customer()
        {
            this.Projects = new HashSet<Project>();
        }

        [DataMember]
        public short CustomerID { get; set; }

        [Required]
        [StringLength(50)]
        [DataType(DataType.EmailAddress)]
        [DataMember]
        public string Email { get; set; }

I am executing the following server side code to successfully get data from my database in a parent/child relationship. The Include method explicity invokes getting the related Project data for the specific Customer. I had to do it this way because you must turn LazyLoading off if you want to get your parent/child data across the wire.

If I look at the WCF messagelog, I can see the actual data coming across in a Customer object and it has the Project object inside of the Customer object.

However, after the call is made and I actually inspect the contents of the “customer” variable, I don’t see any refernces to any Project data.

public IEnumerable<Customer> GetCustomers()
        {
            YeagerTechEntities DbContext = new YeagerTechEntities();

            DbContext.Configuration.ProxyCreationEnabled = false;
            DbContext.Configuration.LazyLoadingEnabled = false;

            IQueryable<Customer> customer = DbContext.Customers.Include("Projects").Where(p => p.CustomerID > 0);

            CloseConnection(DbContext);

            return customer;
        }

The thing I want to do now, is reference the Project data coming back from the call. However, I don’t get any Customer object intellisense after typing “customer.”. It’s all pertains to an IQueryable object.

I’m passing it back into my MVC Controller as the following type:

IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();

and into my View as the following model:

IEnumerable<YeagerTech.YeagerTechWcfService.Customer>

Now, the big question is “How can I reference the Project data coming back in my View?

The below is my code for the View, but there is no intellisense for “item.Project”. Note that “Email” is a property inside my Customer object.

foreach (var item in Model)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:52:56+00:00Added an answer on May 25, 2026 at 1:52 pm

    Actually, after further review, I can now see the Project collection in my Customer collection all the way back up to my client after adding a QucikWatch on the object.

    The correct answer is the last part of my post where the LazyLoadingEnabled = false appears.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that can return either a single object or a collection
I have asked this question about using the a Linq method that returns one
I have the following method: public IEnumerable<Foo> GetFoo(int x, string y) { return from
How do I return a collection of objects from a webmethod? And can this
How can return a variable from a thead (I have the threads handle too).
I have a very complex Linq to SQL query that returns a result set
I have a simple Linq to XML query that i run and the results
I have this, one datatable and one entity class that I can convert to
I have a LINQ to EF query that returns data in a class form.
How can I take the string foo[]=1&foo[]=5&foo[]=2 and return a collection with the values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.