Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello All,

I have list with few items such as "codeproject", "stackoverflow", "msdn". I filter the list using Where and assign to a variable as below,

C#
var output= list.Where(i=>i.Length>5);


but there is nothing in the output.

I cannt figured out, please help me.
Posted
Updated 24-May-12 9:11am
v2
Comments
Sergey Alexandrovich Kryukov 24-May-12 11:39am    
Why? Did you look properly? Run under debugger?
--SA

I'm sure it's there; you only failed to see it by some reason. This will show some output:
C#
string[]
using System.Linq;

//...

list = new string[] {
    "CodeProject", "StackOverflow", "MSDN", "Google", "Bing", "Yahoo", };
var output = list.Where(i => i.Length > 5);
foreach (var item in output)
    System.Console.WriteLine(item);

As expected, on output you will see:
CodeProject
StackOverflow
Google

You can see the same if you make list a list of the type System.Collection.Generic.List<string>. What to try? :-)

—SA
 
Share this answer
 
v3
Comments
Maciej Los 24-May-12 12:17pm    
My 5!
Sergey Alexandrovich Kryukov 24-May-12 12:19pm    
Thank you, Maciej.
--SA
Pete O'Hanlon 25-May-12 4:08am    
The reason he hasn't seen it is that the query hasn't been evaluated. Iterating over the query actually executes it (as does operations like ToList or Skip). This is known as deferred execution, and it can have unfortunate side effects if you aren't aware of it.
Sergey Alexandrovich Kryukov 28-May-12 17:49pm    
Good point, thank you.
--SA
There is nothing in the output at this stage because you haven't actually evaluated the expression. All you've done is create a query expression. You don't get any output from it until you attempt to do something like a First or Take or Count, etc...
 
Share this answer
 
Comments
Mohammad A Rahman 24-May-12 17:23pm    
Exactly as it is the Deferred Execution :)
because of Deferred Execution[^] and Where and Select[^]

Hope it helps :)
 
Share this answer
 
Comments
Pete O'Hanlon 25-May-12 4:05am    
My 5. Most people seem to have missed that the query hasn't actually been executed.
Mohammad A Rahman 25-May-12 4:41am    
Thank you.
What kind of "list" are you using?
I've tried with a List<string> and it works fine

C#
List<string> list = new List<string>();
list.Add("codeproject");
list.Add("stackoverflow");
list.Add("msdn");

var output = list.Where(i => i.Length > 5);

If I execute this code, the ouptput.Count() is 2
 
Share this answer
 
v2
This works perfectly fine for me as expected:
C#
string[] list = {"codeproject","stackoverflow", "msdn" };
var output = list.Where(i => i.Length > 5);
// results 2 strings - codeproject & stackoverflow
 
Share this answer
 
List<string> lste = new List<string>();
lste.Add("codeproject");
lste.Add("stackoverflow");
lste.Add("msdn");

var output = lste.FindAll( i => i.Length > 5);
 
Share this answer
 
v2
The easy solution for you is to convert the Linq result into a list:

var output= list.Where(i=>i.Length>5).ToList();


you can also convert it to an Array with the same result.

What happens is converting it to a List or Array forces immediate execution.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900