Reputation: 1425
I am reading many sites to get a better idea of Linq -Group Join.
var customers = new Customer[]
{
new Customer{Code = 5, Name = "Sam"},
new Customer{Code = 6, Name = "Dave"},
new Customer{Code = 7, Name = "Julia"},
new Customer{Code = 8, Name = "Sue"}
};
// Example orders.
var orders = new Order[]
{
new Order{KeyCode = 5, Product = "Book"},
new Order{KeyCode = 6, Product = "Game"},
new Order{KeyCode = 7, Product = "Computer"},
new Order{KeyCode = 7, Product = "Mouse"},
new Order{KeyCode = 8, Product = "Shirt"},
new Order{KeyCode = 5, Product = "Underwear"}
};
var query = customers.GroupJoin(orders,
c => c.Code,
o => o.KeyCode,
(c, result) => new Result(c.Name, result));//why mention c here??
// Enumerate results.
foreach (var result in query)
{
Console.WriteLine("{0} bought...", result.Name);
foreach (var item in result.Collection)
{
Console.WriteLine(item.Product);
}
}
I couldnt understand why it gives (c, result) ? what if wrote as (c,o) ?
Can anyone share ideas on this?
Upvotes: 2
Views: 246
Reputation: 3082
These are just names of arguments passed to Func
. You can use any name you want if that makes code more clear for you ie:
var query = customers.GroupJoin(orders,
c => c.Code,
o => o.KeyCode,
(something1, something2) => new Result(something1.Name, something2));
as it will just pass arguments from two previous Funcs into last one that is Func<TOuter, IEnumerable<TInner>, TResult>
, so in that case Func<Customer, IEnumerable<Order>, Result>
.
It's the same as with such situation:
public Result DoStuff(Order nameMeAnyWayYouWant, Customer meToo)
{
//do stuff here
}
Code from question is from: http://www.dotnetperls.com/groupjoin
I'm adding model classes that author skipped if anyone wants to elaborate and in case dotnetperls.com went down:
class Customer
{
public int Code { get; set; }
public string Name { get; set; }
}
class Order
{
public int KeyCode { get; set; }
public string Product { get; set; }
}
class Result
{
public string Name { get; set; }
public IEnumerable<Order> Collection { get; set; }
public Result(string name, IEnumerable<Order> collection)
{
this.Name = name;
his.Collection = collection;
}
}
Upvotes: 2