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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:09:07+00:00 2026-06-05T19:09:07+00:00

I’ve read MANY different solutions for the separate functions of LINQ that, when put

  • 0

I’ve read MANY different solutions for the separate functions of LINQ that, when put together would solve my issue. My problem is that I’m still trying to wrap my head about how to put LINQ statements together correctly. I can’t seem to get the syntax right, or it comes up mish-mash of info and not quite what I want.

I apologize ahead of time if half of this seems like a duplicate. My question is more specific than just reading the file. I’d like it all to be in the same query.

To the point though..

I am reading in a text file with semi-colon separated columns of data.

An example would be:

US;Fort Worth;TX;Tarrant;76101
US;Fort Worth;TX;Tarrant;76103
US;Fort Worth;TX;Tarrant;76105
US;Burleson;TX;Tarrant;76097
US;Newark;TX;Tarrant;76071
US;Fort Worth;TX;Tarrant;76103
US;Fort Worth;TX;Tarrant;76105

Here is what I have so far:

var items = (from c in (from line in File.ReadAllLines(myFile)
    let columns = line.Split(';')
    where columns[0] == "US"
    select new
    {
        City = columns[1].Trim(),
        State = columns[2].Trim(),
        County = columns[3].Trim(),
        ZipCode = columns[4].Trim()
    })
    select c);

That works fine for reading the file. But my issue after that is I don’t want the raw data. I want a summary.

Specifically I need the count of the number of occurrences of the City,State combination, and the count of how many times the ZIP code appears.

I’m eventually going to make a tree view out of it.
My goal is to have it laid out somewhat like this:

- Fort Worth,TX (5)
       -  76101 (1)
       -  76103 (2)
       -  76105 (2)  

- Burleson,TX (1)
       - 76097 (1)

- Newark,TX (1)
     - 76071 (1)

I can do the tree thing late because there is other processing to do.

So my question is: How do I combine the counting of the specific values in the query itself? I know of the GroupBy functions and I’ve seen Aggregates, but I can’t get them to work correctly. How do I go about wrapping all of these functions into one query?

EDIT: I think I asked my question the wrong way. I don’t mean that I HAVE to do it all in one query… I’m asking IS THERE a clear, concise, and efficient way to do this with LINQ in one query? If not I’ll just go back to looping through.

If I can be pointed in the right direction it would be a huge help.
If someone has an easier idea in mind to do all this, please let me know.

I just wanted to avoid iterating through a huge array of values and using Regex.Split on every line.

Let me know if I need to clarify.

Thanks!

*EDIT 6/15***

I figured it out. Thanks to those who answered it helped out, but was not quite what I needed. As a side note I ended up changing it all up anyways. LINQ was actually slower than doing it other ways that I won’t go into as it’s not relevent. As to those who made multiple comments on “It’s silly to have it in one query”, that’s the decision of the designer. All “Best Practices” don’t work in all places. They are guidelines. Believe me, I do want to keep my code clear and understandable but I also had a very specific reasoning for doing it the way I did.

I do appreciate the help and direction.

Below is the prototype that I used but later abandoned.

    /* Inner LINQ query Reads the Text File and gets all the Locations.
     * The outer query summarizes this by getting the sum of the Zips 
     * and orders by City/State then ZIP */


var items = from Location in(
    //Inner Query Start
    (from line in File.ReadAllLines(FilePath)
    let columns = line.Split(';')
    where columns[0] == "US" & !string.IsNullOrEmpty(columns[4])
    select new
    {
    City = (FM.DecodeSLIC(columns[1].Trim()) + " " + columns[2].Trim()),
    County = columns[3].Trim(),
                   ZipCode = columns[4].Trim()
    }
    ))
    //Inner Query End
    orderby Location.City, Location.ZipCode  
                   group Location by new { Location.City, Location.ZipCode , Location.County} into grp
            select new
            {
            City = grp.Key.City,
            County = grp.Key.County,
            ZipCode = grp.Key.ZipCode,
            ZipCount = grp.Count()      
            };
  • 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-05T19:09:08+00:00Added an answer on June 5, 2026 at 7:09 pm

    The downside of using File.ReadAllLines is that you have to pull the entire file into memory before operating over it. Also, using Columns[] is a bit clunky. You might want to consider my article describing using DynamicObject and streaming the file as an alternative implemetnation. The grouping/counting operation is secondary to that discussion.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.