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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:37:44+00:00 2026-05-14T09:37:44+00:00

// From my form BindingSource bs = new BindingSource(); private void fillStudentGrid() { bs.DataSource

  • 0

// From my form

BindingSource bs = new BindingSource();

private void fillStudentGrid()
{
     bs.DataSource = Admin.GetStudents();
     dgViewStudents.DataSource = bs;
}

// From the Admin class

public static List<Student> GetStudents()

{
    DojoDBDataContext conn = new DojoDBDataContext();

    var query =
        (from s in conn.Students
         select new Student
         {
             ID = s.ID,
             FirstName = s.FirstName,
             LastName = s.LastName,
             Belt = s.Belt
         }).ToList();

    return query;
}

I’m trying to fill a datagridview control in Winforms, and I only want a few of the values. The code compiles, but throws a runtime error:

Explicit construction of entity type ‘DojoManagement.Student’ in query is not allowed.

Is there a way to get it working in this manner?

  • 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-14T09:37:44+00:00Added an answer on May 14, 2026 at 9:37 am

    You already have an IEnumerable<Student> instance and you cannot project entities from a query for the reasons described here)

    You also don’t need to create a list to bind to this data source – you can greatly simplify your method by changing it to this:

    public static IEnumerable<Student> GetStudents()
    {
        return new DojoDBDataContext().Students;
    }
    

    There is no reason to project a new instance to only map a few of the properties, by executing the query you are returning all the values anyhow and your projection doesn’t save you anything. If you really want to only return a few values from this query for the purposes of information hiding you could do this:

    public static IEnumerable<Object> GetStudents()
    {
        DojoDBDataContext conn = new DojoDBDataContext();
    
        return conn.Students
                   .Select(s => new {
                       ID = s.ID,
                       FirstName = s.FirstName,
                       LastName = s.LastName,
                       Belt = s.Belt 
                   });
    }
    

    Edit: If you aren’t using C# 4 you will have to cast the contents of the IEnumerable<T> to Object explicitly. Only C# 4 supports covariance for IEnumerable<T>. So if you are using C# 3 you will have to do this:

    public static IEnumerable<Object> GetStudents()
    {
        DojoDBDataContext conn = new DojoDBDataContext();
    
        return conn.Students
                   .Select(s => new {
                       ID = s.ID,
                       FirstName = s.FirstName,
                       LastName = s.LastName,
                       Belt = s.Belt 
                   }).Cast<Object>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 487k
  • Answers 487k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your only intent is to make it threadsafe, then… May 16, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer If two threads call GetRand at the same time, the… May 16, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer I suspect you are 'handling' (or more accurately, ignoring) your… May 16, 2026 at 8:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Related Questions

No related questions found

Top Members

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.