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 8480381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:16:47+00:00 2026-06-10T19:16:47+00:00

I have a gridview, the datasource of which is the following function: public static

  • 0

I have a gridview, the datasource of which is the following function:

public static List<Train> GetTrainsByIDs(int [] ids) {
    using (var context = new MyEntities()) 
    {
        return ids.Select(x => context.Trains.Single(y => y.TrainID ==x)).AsQueryable().Include(x=>x.Station).ToList();
    }
}

The grid view has an ItemTemplate of <%# Eval("Station.Name") %>.
This causes the error The ObjectContext instance has been disposed and can no longer be used for operations that require a connection despite the fact that I used the include method.

When I change the function to

public static List<Train> GetTrainsByIDs(int [] ids) {
    using (var context = new MyEntities()) 
    {
        return context.Trains.Where(x => ids.Contains(x.TrainID)).Include(x=>x.Station).ToList();
    }
}

it works fine, but then they come out in the wrong order, and also if I have 2 ids the same I would like 2 identical trains in the list.

Is there anything I can do other than create a new viewmodel? Thank you for any help

  • 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-06-10T19:16:48+00:00Added an answer on June 10, 2026 at 7:16 pm

    After reading @Gert Arnold’s answer, and getting the idea of doing it in 2 stages, I managed very simply using the first query like this:

    using (context = new MyEntities())
    {
        var trns = context.Trains.Include(x => x.Station);
        return ids.Select(x => trns.Single(y => y.TrainID == x)).ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following GridView , which has as DataSource a List<T> : <asp:GridView
I have a Clear button which sets the gridview datasource to null and binds
I have a GridView which is databound to an XML Datasource. For one of
I have a AspxGridView which is linked to a DataSource. Below the GridView there
I have following Gridview: <asp:GridView ID=GridView1 runat=server CssClass=table DataKeyNames=groupId DataSource=<%# dsUserGroupsSelected %> DataMember=Group etc....>
I have a gridview that is supposed to refresh using gridView.DataSource = null; gridView.DataBind();
i have a gridview which is populating from sql datasource defined at designed, i
In my website, I have a gridview which has datasource dynamically bound from database
I have sql dataSource which i have binded with gridview. where i have column
I have gridview and its binded with datasource which is created in the code

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.