Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Groupby Linq and foreach

Tags:

c#

linq

I need a more efficient way of producing multiple files from my data group. Im using a List<MyObject> type and my object has some public properties in which I need to group the data by.

I have heard of Linq and it sounds like something I could use. However Im not sure how to go about it.

I need to produce a text file for each STATE, so grouping all the MyObjects (people) by state, then running a foreach look on them to build the TEXT file.

void Main()
{

    List<MyObject>   lst = new List<MyObject>();
    lst.Add(new MyObject{ name = "bill", state = "nsw", url = "microsoft.com"});
    lst.Add(new MyObject{ name = "ted",  state = "vic", url = "apple.com"});
    lst.Add(new MyObject{ name = "jesse", state = "nsw", url = "google.com"});
    lst.Add(new MyObject{ name = "james", state = "qld", url = "toshiba.com"});

    string builder = "";
    foreach (MyObject item in myObjects)  {

        builder += item.name + "\r\n";
        builder += item.url + "\r\n" + "\r\n\r\n";

    }

and out to the `StreamWriter` will be the filenames by state. 

In total for the above data I need 3 files;

-nsw.txt
-vic.txt
-qld.txt
like image 750
IEnumerable Avatar asked Oct 13 '12 14:10

IEnumerable


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

Something like this, perhaps?

    var groups = lst.GroupBy(x => x.state);

    foreach (var group in groups) 
    {
        using (var f = new StreamWriter(group.Key + ".txt"))
        {
            foreach (var item in group)
            {
                f.WriteLine(item.name);
                f.WriteLine(item.url);
            }
        }
    }
like image 155
lesscode Avatar answered Oct 22 '22 10:10

lesscode


You def. could use LINQ here.

lst.GroupBy(r=> r.state).ToList().ForEach(r=> {
        //state= r.Key
        //

        foreach (var v in r)
        {

        }
    });

The thing about linq. If you want to know how to do something in it. Think "how would I do this in SQL". The keywords are for the most part the same.

like image 25
iamkrillin Avatar answered Oct 22 '22 10:10

iamkrillin


You can actually produce entire content with LINQ:

var entryFormat = "{1}{0}{2}{0}{0}{0}";
var groupsToPrint = lst
   .GroupBy(p => p.state)
   .Select(g => new 
    {
       State = g.Key,
       // produce file content on-the-fly from group entries
       Content = string.Join("", g.Select(v => string.Format(entryFormat, 
           Environment.NewLine, v.name, v.url)))
    });

var fileNameFormat = "{0}.txt";
foreach (var entry in groupsToPrint)
{
    var fileName = string.Format(fileNameFormat, entry.State);
    File.WriteAllText(fileName, entry.Content);
}
like image 3
k.m Avatar answered Oct 22 '22 11:10

k.m