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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:17:20+00:00 2026-06-12T20:17:20+00:00

I am passing values into a method which uses a foreach loop to iterate

  • 0

I am passing values into a method which uses a foreach loop to iterate through a collection. In the loop, an Include statement is used from entity framework to eager load. This is what I pass in:

var exp = new Collection<Expression<Func<Foo,object>>>();

Why is it that when I use this:

exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Position)));
exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Bank)));

and Employee, Position, and Bank all have the field Name that it will jumble the Name between the different fields? As in, Bank and Position will both have Employee’s name in their Name field. To be more explicit, for whatever reason

In DataBase:

Employee.Name = "Jon";
Employee.Bank.Name = "World Bank";
Employee.Position.Name = "CEO";

Data from .Include:

Employee.Bank.Name == "Jon" //true
Employee.Position.Name == "Jon" //true

Extra Information, inside of method that accepts exp

DbSet<Foo> dbSet = context.Set<Foo>();
IQueryable<Foo> query = dbSet;

if (exp != null)
{
 foreach (var incProp in exp)
 {
  query = query.Include(incProp);
 }
}

Am I doing something wrong in my code?

edit

public class Foo
{
 public int FooId { get; set; }
 public virtual List<Bar> Bars { get; set; }
}

public class Bar
{
 public int BarId { get; set; }
 public virtual Foo Foo { get; set; }
 public int FooId { get; set; }
 public virtual List<Employee> Employees { get; set; }
}

public class Employee
{
 public int EmployeeId { get; set; }
 public int BarId { get; set; }
 public virtual Bar Bar { get; set; }
 public int BankId { get; set; }
 public virtual Bank Bank { get; set; }
 public int PositionId { get; set; }
 public virtual Position Position { get; set; }
 public string Name { get; set; }
}

public class Bank
{
 public int BankId { get; set; }
 public string Name { get; set; }
}

public class Position
{
 public int PositionId { get; set; }
 public string Name { get; set; }
}
  • 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-12T20:17:22+00:00Added an answer on June 12, 2026 at 8:17 pm

    I don’t think the problem is in the code you’re showing. I created a console app from what you’ve put above and it output what was in the database. Here’s the entirety of the app:

    namespace ExampleCF
    {
        public class Foo
        {
            public int FooId { get; set; }
            public virtual List<Bar> Bars { get; set; }
        }
    
        public class Bar
        {
            public int BarId { get; set; }
            public virtual Foo Foo { get; set; }
            public int FooId { get; set; }
            public virtual List<Employee> Employees { get; set; }
        }
    
        public class Employee
        {
            public int EmployeeId { get; set; }
            public int BarId { get; set; }
            public virtual Bar Bar { get; set; }
            public int BankId { get; set; }
            public virtual Bank Bank { get; set; }
            public int PositionId { get; set; }
            public virtual Position Position { get; set; }
            public string Name { get; set; }
        }
    
        public class Bank
        {
            public int BankId { get; set; }
            public string Name { get; set; }
        }
    
        public class Position
        {
            public int PositionId { get; set; }
            public string Name { get; set; }
        }
    
        public class Model : DbContext
        {
            public DbSet<Foo> Foos { get; set; }
            public DbSet<Bar> Bars { get; set; }
            public DbSet<Employee> Employees { get; set; }
            public DbSet<Bank> Banks { get; set; }
            public DbSet<Position> Positions { get; set; }
    
            public Model()
            {
                Configuration.LazyLoadingEnabled = false;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Model context = new Model();
                var exp = new Collection<Expression<Func<Foo, object>>>();
    
                Foo foo = new Foo();
                Bar bar = new Bar();
                Employee emp = new Employee() { Name = "employee" };
                Bank bank = new Bank() { Name = "bank" };
                Position position = new Position() { Name = "position" };
                foo.Bars = new List<Bar>();
                foo.Bars.Add(bar);
                bar.Employees = new List<Employee>();
                bar.Employees.Add(emp);
                emp.Position = position;
                emp.Bank = bank;
                context.Foos.Add(foo);
                context.SaveChanges();
    
                exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Position)));
                exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Bank)));
    
                DbSet<Foo> dbSet = context.Set<Foo>();
                IQueryable<Foo> query = dbSet;
    
                if (exp != null)
                {
                    foreach (var incProp in exp)
                    {
                        query = query.Include(incProp);
                    }
                }
    
                var first = query.ToList().FirstOrDefault();
                var firstEmp = first.Bars.First().Employees.First();
                Console.WriteLine(String.Format("{0} | {1} | {2}", firstEmp.Name, firstEmp.Bank.Name, firstEmp.Position.Name));
            }
        }
    
    }
    

    outputs: employee | bank |position

    Is there anything else you’re adding to the query, or maybe you’re somehow creating an anonymous type?

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

Sidebar

Related Questions

Curently I'm passing my const string values up from my C++ into my C#
I'm using variable passing through a URL (ie .derp.html?name=derp?lname=herp) to a popup (which uses
I have an IF statement that consists of two separate function calls passing values
I've written a method to turn a hash (nested if necessary) of values into
I am having trouble nailing down the best method for passing data from a
I've got a method similar to this, which loops through one set of data
I have a method where I am passing in two object, which have the
I want to add an additional value into an array before passing it to
I want to know what the best practice is for passing values (sometimes multiple
I am passing dynamic values with href tag in dynamically generated jnlp using jsp.

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.