tags:

views:

87

answers:

4

I am writing a Linq query just for learning.

    var result = Process.GetProcesses()
                .Where(p => p.WorkingSet64 > 20 * 1024 * 1024)
                .OrderByDescending(p => p.WorkingSet64)
                .Select(p => new {p.Id,p.ProcessName,p.WorkingSet64 });

I want to iterate in to result

 foreach(process in result) //error-type or namespace process could not be found.
            {
                Console.WriteLine(Process.ProcessName);
            }

I want to iterate in to result and print each process name on the console.

What I am doing wrong.

+8  A: 

you're close:

foreach (var process in result) {
    Console.WriteLine(process.ProcessName);
}

(I'm assuming you haven't declared the name process before.)

Also note that if you use process (with a small p) in the foreach line, you need to keep the same case when you use it inside the loop.

Jon
In this case, he has no choice but to use var, because he's using an anonymous type.
Blorgbeard
@Blorgbeard good point!
Jon
+1  A: 

Are you declared process before loop? If not you should change your for each to

foreach(var process in result)
{
    Console.WriteLine(process.ProcessName);
}
DixonD
It wouldn't be possible to declare `process`, from the LINQ query, it's an anonymous type.
Blorgbeard
@Blorgbeard Yes, you are right here
DixonD
and need to change the first 'P' inside the WriteLine to 'p' :)
Rune FS
@Rune FS Changed before you commented:)
DixonD
A: 

You could also project your query into a list of processes:

List<Process> result = Process.GetProcesses()
              .Where(p => p.WorkingSet64 > 20 * 1024 * 1024)
              .OrderByDescending(p => p.WorkingSet64).ToList();

foreach(Process process in result) 
{
  Console.WriteLine(process.ProcessName);
}
Stephan Keller
It's objects of an anonymous type created in the Select part there's no Process type in the result
Rune FS
@Rune, He's suggesting that instead of creating an anonymous type, he should directly use the `Process` objects since they were already instantiated and contain all the data the OP needs. Nothing wrong with that.
Allon Guralnek
@Allon first of all there's nowhere stated that the return type of Process.GetProcess is of the type IEnumerable<Process> only IEnumerable<T> (all though I agree that would seem reasonable) and secondly OP wants to learn so helping him understand the code he's posting rather than giving him some code that might do the same would in my world fit as an answer but I'd tkae your note and agree that maybe down voting was a bit harsh so I'll redo that when I'm allowed to
Rune FS
@Rune, What do you mean by "nowhere stated"? The return type is `System.Diagnostics.Process[]`, as stated [in this MSDN article](http://msdn.microsoft.com/en-us/library/1f3ys1f9.aspx).
Allon Guralnek
@Rune: I agree to downvoting on the grounds that I did not really help the OP in learning what he was doing wrong. My bad, I was looking more at his code fragment instead of reading the question more carfully. However I will not agree to your first comment because my answer does not create an anonymous type but a List<T> of "Process".
Stephan Keller
@Allon that is you assumption that it's System.Diagnostics.Process that OP is using. you might be very correct in that assumption but it's an assumption none the less
Rune FS
@Stephan my bad for not being particularly clear I'm referring to the code of OP in my first comment :)
Rune FS
@Rune, are you kidding me? Of course it's a `System.Diagnostics.Process`. What are the chances that it's some other class that happens to have the same `Id`, `ProcessName` and `WorkingSet64` properties and is returned by a method that has the identical name of `Process.GetProcesses()` which happens to return an array of the `Process` doppelganger? I mean, really?
Allon Guralnek
+1  A: 

Consider using LINQ's query syntax for terser code:

var result = from p in Process.GetProcesses()
             where p.WorkingSet64 > 20 * 1024 * 1024
             orderby p.WorkingSet64 descending
             select new { p.Id, p.ProcessName, p.WorkingSet64 };

Then, instead of a loop, think in LINQ to do the same thing:

Console.WriteLine(string.Join("\r\n", result.Select(p => p.ProcessName)));

EDIT: The overload of string.Join() used above was only introduced in .NET 4.0. To use an overload that is available in earlier versions, which accepts a string[] rather than an IEnumerable<string>, just chain a .ToArray() after the .Select():

Console.WriteLine(string.Join("\r\n", result.Select(p => p.ProcessName).ToArray()));
Allon Guralnek