linq queries

T

Tony Johansson

Hello!

I have two linq queries below and I wonder if the
second one will give the same result as the first one.

var orders = db.Customers
..Where(c => c.Country == "USA" && c.Region == "WA")
..SelectMany(c =>c.Orders);

var orders =
from c in db.Customers
where c.Country == "USA" && c.Region == "WA"
select c.Orders;

//Tony
 
M

Martin Honnen

Tony said:
I have two linq queries below and I wonder if the
second one will give the same result as the first one.

var orders = db.Customers
.Where(c => c.Country == "USA" && c.Region == "WA")
.SelectMany(c =>c.Orders);

var orders =
from c in db.Customers
where c.Country == "USA" && c.Region == "WA"
select c.Orders;

Check the type you get, I think it is different.
I think you would need
var orders =
from c in db.Customers
where c.Country == "USA" && c.Region == "WA"
from o in c.Orders
select o;
as the query syntax to match the method syntax query.
 
A

Anthony Jones

Tony Johansson said:
Hello!

I have two linq queries below and I wonder if the
second one will give the same result as the first one.

var orders = db.Customers
.Where(c => c.Country == "USA" && c.Region == "WA")
.SelectMany(c =>c.Orders);

var orders =
from c in db.Customers
where c.Country == "USA" && c.Region == "WA"
select c.Orders;

Yes these are the same.
 
A

Anthony Jones

Martin Honnen said:
Check the type you get, I think it is different.
I think you would need
var orders =
from c in db.Customers
where c.Country == "USA" && c.Region == "WA"
from o in c.Orders
select o;
as the query syntax to match the method syntax query.

The type from this expression would be IEnumerable<order> whereas the type
of both statements expressions in the OP is IEnumerable<orders>.

IOW, a sequence of collections of orders, not a sequence of all the
individual orders.
 
M

Martin Honnen

Anthony said:
The type from this expression would be IEnumerable<order> whereas the
type of both statements expressions in the OP is IEnumerable<orders>.

I am not good at thinking about data structures without seeing the
details (e.g. definition of db.Customers) but I am pretty sure the
SelectMany in the first query of the OP flattens the IEnumerable<orders>
to IEnumerable<order>.

For instance assuming these two class definitions

public class Customer
{
public string Name { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int OrderId { get; set; }
}

the following code that uses explicit types compiles fine:

List<Customer> customers = new List<Customer>();

IEnumerable<Order> orders1 = customers
.Where(c => c.Country == "USA" && c.Region == "WA")
.SelectMany(c => c.Orders);

IEnumerable<List<Order>> orders2 =
from c in customers
where c.Country == "USA" && c.Region == "WA"
select c.Orders;

IEnumerable<Order> orders =
from c in customers
where c.Country == "USA" && c.Region == "WA"
from o in c.Orders
select o;

So the two queries by the original poster are not the same, the
SelectMany makes a difference.
 
A

Anthony Jones

Martin Honnen said:
I am not good at thinking about data structures without seeing the details
(e.g. definition of db.Customers) but I am pretty sure the SelectMany in
the first query of the OP flattens the IEnumerable<orders> to
IEnumerable<order>.

For instance assuming these two class definitions

public class Customer
{
public string Name { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int OrderId { get; set; }
}

the following code that uses explicit types compiles fine:

List<Customer> customers = new List<Customer>();

IEnumerable<Order> orders1 = customers
.Where(c => c.Country == "USA" && c.Region == "WA")
.SelectMany(c => c.Orders);

IEnumerable<List<Order>> orders2 =
from c in customers
where c.Country == "USA" && c.Region == "WA"
select c.Orders;

IEnumerable<Order> orders =
from c in customers
where c.Country == "USA" && c.Region == "WA"
from o in c.Orders
select o;

So the two queries by the original poster are not the same, the SelectMany
makes a difference.

Martin, You're absolutely right, I wasn't readling the Docs on SelectMany
properly. It does exactly what you say it does.
 

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