about some understanding with Linq

T

Tony Johansson

Hello!

Assume I have this Linq query.
var items =
from s in greetings
where s.EndsWith("LINQ")
select s;

foreach(var item in items)
Console.WriteLine(item);

Now to my question I read in a Linq book called Pro LINQ.
The bool is saying LINQ queries are often deferred and not actually executed
when it appears you are calling
them. In my example above if I put a breakpoint on this row "foreach(var
item in items)"
and examine the items varable will this then contain any data.

I don't really understand when this variable items will be filled with data
?

//Tony
 
S

Sebastian Hungerecker

Tony said:
Hello! Hello.

Assume I have this Linq query.
var items =
from s in greetings
where s.EndsWith("LINQ")
select s;

foreach(var item in items)
Console.WriteLine(item);

[...]

I don't really understand when this variable items will be filled with
data ?

It won't. The items variable holds an IEnumerable<T> object which does not
itself contain any data. The query will be executed when you enter the
foreach loop (or more generally: when you directly or indirectly call
items' GetEnumerator() method), but even after the loop, the items variable
will not itself contain any data. Every time you loop over items the query
will be executed again (taking into account any change to greetings that
might have happened in the mean time).
Maybe this example will be helpful for you:
using System;
using System.Linq;
using System.Collections.Generic;

namespace test
{
static class MainClass {
public static void Main() {
List<String> greetings = new List<string>() {"foo", "bar
LINQ", "baz"};
var items = from s in greetings
where s.EndsWith("LINQ")
select s;

IList<String> foo = items.ToList();

Console.WriteLine("Iterate items before changing the original
list:");
foreach(var item in items)
Console.WriteLine(item);

greetings.Add("bay LINQ");
Console.WriteLine("Iterate items after changing the original
list:");
foreach(var item in items)
Console.WriteLine(item);

Console.WriteLine("Iterate over foo:");
foreach(var item in foo)
Console.WriteLine(item);
}
}
}
Output:
Iterate items before changing the original list:
bar LINQ
Iterate items after changing the original list:
bar LINQ
bay LINQ
Iterate over foo:
bar LINQ

HTH,
Sebastian
 
T

Tony Johansson

Hello!

I just wonder about this statement IList<String> foo = items.ToList();
Here as you said items doesn't contain any data but in this case
what happen here. Can you expalin that!

//Tony

Sebastian Hungerecker said:
Tony said:
Hello! Hello.

Assume I have this Linq query.
var items =
from s in greetings
where s.EndsWith("LINQ")
select s;

foreach(var item in items)
Console.WriteLine(item);

[...]

I don't really understand when this variable items will be filled with
data ?

It won't. The items variable holds an IEnumerable<T> object which does not
itself contain any data. The query will be executed when you enter the
foreach loop (or more generally: when you directly or indirectly call
items' GetEnumerator() method), but even after the loop, the items
variable
will not itself contain any data. Every time you loop over items the query
will be executed again (taking into account any change to greetings that
might have happened in the mean time).
Maybe this example will be helpful for you:
using System;
using System.Linq;
using System.Collections.Generic;

namespace test
{
static class MainClass {
public static void Main() {
List<String> greetings = new List<string>() {"foo", "bar
LINQ", "baz"};
var items = from s in greetings
where s.EndsWith("LINQ")
select s;

IList<String> foo = items.ToList();

Console.WriteLine("Iterate items before changing the original
list:");
foreach(var item in items)
Console.WriteLine(item);

greetings.Add("bay LINQ");
Console.WriteLine("Iterate items after changing the original
list:");
foreach(var item in items)
Console.WriteLine(item);

Console.WriteLine("Iterate over foo:");
foreach(var item in foo)
Console.WriteLine(item);
}
}
}
Output:
Iterate items before changing the original list:
bar LINQ
Iterate items after changing the original list:
bar LINQ
bay LINQ
Iterate over foo:
bar LINQ

HTH,
Sebastian
 
S

Sebastian Hungerecker

Tony said:
I just wonder about this statement IList<String> foo = items.ToList();
Here as you said items doesn't contain any data but in this case
what happen here. Can you expalin that!

Sure. The above line calls the ToList() method on items. The ToList methods
iterates over the receiver (items) to create a list containing the iterated
elements. So when that happens the query stored in items is executed (as it
is executed each time someone iterates over items). The results of that are
now stored in foo and are independent of any subsequent changes to items or
greetings.

HTH,
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top