linq queries

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
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.
 
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.
 
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.
 
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.
 
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.
 
Back
Top