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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:17:58+00:00 2026-06-16T18:17:58+00:00

Trying to loop through a list of objects using a foreach loop and I’ve

  • 0

Trying to loop through a list of objects using a foreach loop and I’ve come into trouble as I get a compiler error

Foreach statement cannot operate on variable object because it does not contain a definition for GetEnumerator.

I parse a file to an anonymous List and then use that list in a method.

     var list = (from s in File.ReadAllLines(path).Select(a => a.Split(new[] { '|' }, StringSplitOptions.None))
                     select new
                     {
                         Nbr = s[0].Trim(),
                         Name = s[1].Trim(),
                         Phone = s[2].Trim(),
                         Addr = s[3].Trim()
                     }).ToList();
 findmatch(list);
}
  Public static void findmatch(object list)
 {
  foreach(var entry in list)

I also tried to change the code around like this:

foreach(var entry in list.GetType().GetProperties()) as I still get this error

Sorry yes I made an edit to recognize that I’m carrying list over to another method.

  • 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-16T18:18:00+00:00Added an answer on June 16, 2026 at 6:18 pm

    Alright, now that you have shown your real code it’s clear where the problem is. Anonymous objects cannot leave the local context. You cannot loop over an instance of an object. And you cannot use this anonymous object anywhere outside of the context in which this anonymous object is defined in.

    So start by defining a model:

    public class MyModel
    {
        public string Nbr { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Adr { get; set; }
    }
    

    then adapt your LINQ query to return an IEnumerable of this model instead of some anonymous object:

    var list = 
        from s in File.ReadAllLines(path).Select(a => a.Split(new[] { '|' }, StringSplitOptions.None))
        select new MyModel
        {         
            Nbr = s[0].Trim(),
            Name = s[1].Trim(),
            Phone = s[2].Trim(),
            Addr = s[3].Trim()
        };
    findmatch(list);
    

    and finally adapt your findmatch method to take an IEnumerable of your model instead of some weakly typed object which you cannot iterate over anyways:

    public static void findmatch(IEnumerable<MyModel> list)
    {
        foreach(var entry in list)
        {
            // do whatever you intended to do with the entry previously
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to loop through a list of files to get the path and
I'm trying to loop through a list of results from MySQL and display all
I'm trying to loop through a text file with a list of urls and
Trying to do it through a loop that traverses through the list. In the
I'm trying to loop through objects from my database in the template and one
I have a foreach looping through a List of objects in an ASP.NET MVC/Razor
I'm trying to loop through a list (csv) containing two fields; a name and
I'm trying to loop through all files of .properties extension, in a root folder:C:\ExecutionSDKTest_10.2.2\,
I'm trying to loop through a number of items, and create a json object.
I am trying to loop through a directory of text files and combine them

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.