Linq to objects and lambda expression












1














There are classes Client and Commande :



public class Client {
public int Identifiant { get; set; }
public string Nom { get; set; }
public int Age { get; set; }
}

public class Commande
{
public int Identifiant {get; set;}
public int IdentifiantClient {get;set;}
public decimal Prix {get;set;}
}


Here are some lists based on these classes :



List<Client> listeClients = new List<Client>
{ new Client{Identifiant=1,Nom="Nicolas",Age=30},
new Client{ Identifiant = 2, Nom = "Jérémie", Age = 20},
new Client{ Identifiant=3, Nom="Delphine", Age=30},
new Client{Identifiant = 4, Nom = "Bob", Age = 10}
};

List<Commande> listeCommandes = new List <Commande>
{
new Commande{Identifiant=1, IdentifiantClient=1, Prix = 150.05M},
new Commande{Identifiant=2, IdentifiantClient= 2, Prix= 30M},
new Commande{Identifiant= 3, IdentifiantClient= 1, Prix= 99.99M},
new Commande{Identifiant= 4, IdentifiantClient= 1, Prix= 100M},
new Commande{Identifiant= 5, IdentifiantClient = 3, Prix = 80M},
new Commande{Identifiant = 6, IdentifiantClient = 3,Prix = 10M}
};


Now there is this Linq expression :



var liste = from commande in listeCommandes
join
client in listeClients on commande.IdentifiantClient equals client.Identifiant
group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees
let total = commandesGroupees.Sum(c => c.Prix)
where total > 50
orderby total
select new {
commandesGroupees.Key.IdentifiantClient,
commandesGroupees.Key.Nom,
NombreDeCommandes = commandesGroupees.Count(),
PrixTotal = total
};

foreach(var element in liste)
{
Console.WriteLine("Le client {0} ({1}) a réalisé {2} commande(s) pour un total de {3}", element.Nom, element.IdentifiantClient, element.NombreDeCommandes, element.PrixTotal);
}


In the Linq expression there is this expression : let total = commandesGroupees.Sum(c => c.Prix) . How is it possible that the parameter c represents an instanciation of the Command class ? because there is the calling of the property Prix !










share|improve this question






















  • I dont understand why the object is an instance of the class Commande.
    – pheromix
    Nov 13 '18 at 6:35










  • Are you asking how lambda works, or asking why it's that and not something else?
    – John
    Nov 13 '18 at 6:36










  • I am asking why it is that and not something else ( the class Client instance for example).
    – pheromix
    Nov 13 '18 at 6:37


















1














There are classes Client and Commande :



public class Client {
public int Identifiant { get; set; }
public string Nom { get; set; }
public int Age { get; set; }
}

public class Commande
{
public int Identifiant {get; set;}
public int IdentifiantClient {get;set;}
public decimal Prix {get;set;}
}


Here are some lists based on these classes :



List<Client> listeClients = new List<Client>
{ new Client{Identifiant=1,Nom="Nicolas",Age=30},
new Client{ Identifiant = 2, Nom = "Jérémie", Age = 20},
new Client{ Identifiant=3, Nom="Delphine", Age=30},
new Client{Identifiant = 4, Nom = "Bob", Age = 10}
};

List<Commande> listeCommandes = new List <Commande>
{
new Commande{Identifiant=1, IdentifiantClient=1, Prix = 150.05M},
new Commande{Identifiant=2, IdentifiantClient= 2, Prix= 30M},
new Commande{Identifiant= 3, IdentifiantClient= 1, Prix= 99.99M},
new Commande{Identifiant= 4, IdentifiantClient= 1, Prix= 100M},
new Commande{Identifiant= 5, IdentifiantClient = 3, Prix = 80M},
new Commande{Identifiant = 6, IdentifiantClient = 3,Prix = 10M}
};


Now there is this Linq expression :



var liste = from commande in listeCommandes
join
client in listeClients on commande.IdentifiantClient equals client.Identifiant
group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees
let total = commandesGroupees.Sum(c => c.Prix)
where total > 50
orderby total
select new {
commandesGroupees.Key.IdentifiantClient,
commandesGroupees.Key.Nom,
NombreDeCommandes = commandesGroupees.Count(),
PrixTotal = total
};

foreach(var element in liste)
{
Console.WriteLine("Le client {0} ({1}) a réalisé {2} commande(s) pour un total de {3}", element.Nom, element.IdentifiantClient, element.NombreDeCommandes, element.PrixTotal);
}


In the Linq expression there is this expression : let total = commandesGroupees.Sum(c => c.Prix) . How is it possible that the parameter c represents an instanciation of the Command class ? because there is the calling of the property Prix !










share|improve this question






















  • I dont understand why the object is an instance of the class Commande.
    – pheromix
    Nov 13 '18 at 6:35










  • Are you asking how lambda works, or asking why it's that and not something else?
    – John
    Nov 13 '18 at 6:36










  • I am asking why it is that and not something else ( the class Client instance for example).
    – pheromix
    Nov 13 '18 at 6:37
















1












1








1







There are classes Client and Commande :



public class Client {
public int Identifiant { get; set; }
public string Nom { get; set; }
public int Age { get; set; }
}

public class Commande
{
public int Identifiant {get; set;}
public int IdentifiantClient {get;set;}
public decimal Prix {get;set;}
}


Here are some lists based on these classes :



List<Client> listeClients = new List<Client>
{ new Client{Identifiant=1,Nom="Nicolas",Age=30},
new Client{ Identifiant = 2, Nom = "Jérémie", Age = 20},
new Client{ Identifiant=3, Nom="Delphine", Age=30},
new Client{Identifiant = 4, Nom = "Bob", Age = 10}
};

List<Commande> listeCommandes = new List <Commande>
{
new Commande{Identifiant=1, IdentifiantClient=1, Prix = 150.05M},
new Commande{Identifiant=2, IdentifiantClient= 2, Prix= 30M},
new Commande{Identifiant= 3, IdentifiantClient= 1, Prix= 99.99M},
new Commande{Identifiant= 4, IdentifiantClient= 1, Prix= 100M},
new Commande{Identifiant= 5, IdentifiantClient = 3, Prix = 80M},
new Commande{Identifiant = 6, IdentifiantClient = 3,Prix = 10M}
};


Now there is this Linq expression :



var liste = from commande in listeCommandes
join
client in listeClients on commande.IdentifiantClient equals client.Identifiant
group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees
let total = commandesGroupees.Sum(c => c.Prix)
where total > 50
orderby total
select new {
commandesGroupees.Key.IdentifiantClient,
commandesGroupees.Key.Nom,
NombreDeCommandes = commandesGroupees.Count(),
PrixTotal = total
};

foreach(var element in liste)
{
Console.WriteLine("Le client {0} ({1}) a réalisé {2} commande(s) pour un total de {3}", element.Nom, element.IdentifiantClient, element.NombreDeCommandes, element.PrixTotal);
}


In the Linq expression there is this expression : let total = commandesGroupees.Sum(c => c.Prix) . How is it possible that the parameter c represents an instanciation of the Command class ? because there is the calling of the property Prix !










share|improve this question













There are classes Client and Commande :



public class Client {
public int Identifiant { get; set; }
public string Nom { get; set; }
public int Age { get; set; }
}

public class Commande
{
public int Identifiant {get; set;}
public int IdentifiantClient {get;set;}
public decimal Prix {get;set;}
}


Here are some lists based on these classes :



List<Client> listeClients = new List<Client>
{ new Client{Identifiant=1,Nom="Nicolas",Age=30},
new Client{ Identifiant = 2, Nom = "Jérémie", Age = 20},
new Client{ Identifiant=3, Nom="Delphine", Age=30},
new Client{Identifiant = 4, Nom = "Bob", Age = 10}
};

List<Commande> listeCommandes = new List <Commande>
{
new Commande{Identifiant=1, IdentifiantClient=1, Prix = 150.05M},
new Commande{Identifiant=2, IdentifiantClient= 2, Prix= 30M},
new Commande{Identifiant= 3, IdentifiantClient= 1, Prix= 99.99M},
new Commande{Identifiant= 4, IdentifiantClient= 1, Prix= 100M},
new Commande{Identifiant= 5, IdentifiantClient = 3, Prix = 80M},
new Commande{Identifiant = 6, IdentifiantClient = 3,Prix = 10M}
};


Now there is this Linq expression :



var liste = from commande in listeCommandes
join
client in listeClients on commande.IdentifiantClient equals client.Identifiant
group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees
let total = commandesGroupees.Sum(c => c.Prix)
where total > 50
orderby total
select new {
commandesGroupees.Key.IdentifiantClient,
commandesGroupees.Key.Nom,
NombreDeCommandes = commandesGroupees.Count(),
PrixTotal = total
};

foreach(var element in liste)
{
Console.WriteLine("Le client {0} ({1}) a réalisé {2} commande(s) pour un total de {3}", element.Nom, element.IdentifiantClient, element.NombreDeCommandes, element.PrixTotal);
}


In the Linq expression there is this expression : let total = commandesGroupees.Sum(c => c.Prix) . How is it possible that the parameter c represents an instanciation of the Command class ? because there is the calling of the property Prix !







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 6:20









pheromix

5,470155091




5,470155091












  • I dont understand why the object is an instance of the class Commande.
    – pheromix
    Nov 13 '18 at 6:35










  • Are you asking how lambda works, or asking why it's that and not something else?
    – John
    Nov 13 '18 at 6:36










  • I am asking why it is that and not something else ( the class Client instance for example).
    – pheromix
    Nov 13 '18 at 6:37




















  • I dont understand why the object is an instance of the class Commande.
    – pheromix
    Nov 13 '18 at 6:35










  • Are you asking how lambda works, or asking why it's that and not something else?
    – John
    Nov 13 '18 at 6:36










  • I am asking why it is that and not something else ( the class Client instance for example).
    – pheromix
    Nov 13 '18 at 6:37


















I dont understand why the object is an instance of the class Commande.
– pheromix
Nov 13 '18 at 6:35




I dont understand why the object is an instance of the class Commande.
– pheromix
Nov 13 '18 at 6:35












Are you asking how lambda works, or asking why it's that and not something else?
– John
Nov 13 '18 at 6:36




Are you asking how lambda works, or asking why it's that and not something else?
– John
Nov 13 '18 at 6:36












I am asking why it is that and not something else ( the class Client instance for example).
– pheromix
Nov 13 '18 at 6:37






I am asking why it is that and not something else ( the class Client instance for example).
– pheromix
Nov 13 '18 at 6:37














2 Answers
2






active

oldest

votes


















2














Let's analyse this query expression bit by bit.



from commande in listeCommandes


It is clear that if listeCommandes is a List<Commande>, commande should be of type Commande. Right?



join
client in listeClients on commande.IdentifiantClient equals client.Identifiant


This join joins the client list to the query. Now we have a client variable of type Client. But this doesn't have much to do with commande, which is still of type Commande.



group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees


This is where it gets interesting. When you group commande, a bunch of IGrouping<AnonymousClass, Commande> are created. This is exactly the commandesGroupees here.



So what happens if you call Sum on a IGrouping<AnonymousClass, Commande>? Remember that Sum is an extension method on IEnumerable<T>, and IGrouping<AnonymousClass, Commande> implements IEnumerable<Commandes>. This means that you are basically calling Sum on a IEnumerable<Commandes>, which expects a Function<Commande, decimal>.






share|improve this answer





















  • So the first variable in the query gets the priority of the instance ?
    – pheromix
    Nov 13 '18 at 7:07






  • 1




    @pheromix In the case of group by, yes. Instead of thinking about first variable and second variable, think about this logically. I ask you to group a bunch of blocks by their color, and you will produce a bunch of groups, each of which has a bunch of blocks in it and the blocks in each group all have the same color, right? That's an IGrouping<Color, Block>. Now imagine I ask you to group the blocks by X. You will end up with a bunch of IGrouping<X, Block>. Make sense?
    – Sweeper
    Nov 13 '18 at 7:13






  • 1




    @pheromix Notice how whatever X is, the second argument of IGrouping is always Block, because you are grouping blocks. X is just a criteria you group by. The groups won't end up containing something else just because X is something else.
    – Sweeper
    Nov 13 '18 at 7:16






  • 1




    @pheromix In words, your code is saying this: group the commandes by both the IdentifiantClient and the Nom of the client who is joined to that commande.
    – Sweeper
    Nov 13 '18 at 7:22






  • 2




    @pheromix Yes. That's what I said in the first sentence of my first comment.
    – Sweeper
    Nov 13 '18 at 7:27



















2














Let's chunk it in smaller bits to make it simpler



from commande in listeCommandes 


commande is of type Commande



group commande by new { commande.IdentifiantClient, client.Nom } 


This is probably the bit confusing you, this still creates an IEnumerable<Commande> not an IEnumerable<anonymous<identifiantclient,nom>>, the new is what you want to group by, not what you want the grouped items to be, at this point you're creating an IGrouping that has IdentifiantClient and Nom as key but has an IEnumerable has an element



into commandesGroupees


Gives a name to the IGrouping we previously created



let total = commandesGroupees.Sum(c => c.Prix)


An IGrouping<TKey,TValue> is an IEnumerable<TValue> so as we've seen earlier each of the groups (grouped by idenfiantClient and Nom) hold a sequence of commands, since we're doing a linq query on it (sum) we're not concerned about the key but about the IEnumerable that is exposed (the IEnumerable<Command>).



This is why the sum works on Command and those have a Prix property.






share|improve this answer





















  • I am not still convinced.
    – pheromix
    Nov 13 '18 at 7:03










  • @pheromix You'll have to explain what isn't clear, that's just exactly how this works so if you don't get it you need to tell me which part isn't clear so i can better elaborate.
    – Ronan Thibaudau
    Nov 13 '18 at 7:10










  • why is it not based on the Client class ?
    – pheromix
    Nov 13 '18 at 7:12






  • 1




    @pheromix, tu as group commande, le groupe est donc un group de Commande. group client donnera un groupe de Client
    – Drag and Drop
    Nov 13 '18 at 7:15












  • So the group keyword tells what is the instance ? but not the variable in the query ?
    – pheromix
    Nov 13 '18 at 7:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274956%2flinq-to-objects-and-lambda-expression%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Let's analyse this query expression bit by bit.



from commande in listeCommandes


It is clear that if listeCommandes is a List<Commande>, commande should be of type Commande. Right?



join
client in listeClients on commande.IdentifiantClient equals client.Identifiant


This join joins the client list to the query. Now we have a client variable of type Client. But this doesn't have much to do with commande, which is still of type Commande.



group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees


This is where it gets interesting. When you group commande, a bunch of IGrouping<AnonymousClass, Commande> are created. This is exactly the commandesGroupees here.



So what happens if you call Sum on a IGrouping<AnonymousClass, Commande>? Remember that Sum is an extension method on IEnumerable<T>, and IGrouping<AnonymousClass, Commande> implements IEnumerable<Commandes>. This means that you are basically calling Sum on a IEnumerable<Commandes>, which expects a Function<Commande, decimal>.






share|improve this answer





















  • So the first variable in the query gets the priority of the instance ?
    – pheromix
    Nov 13 '18 at 7:07






  • 1




    @pheromix In the case of group by, yes. Instead of thinking about first variable and second variable, think about this logically. I ask you to group a bunch of blocks by their color, and you will produce a bunch of groups, each of which has a bunch of blocks in it and the blocks in each group all have the same color, right? That's an IGrouping<Color, Block>. Now imagine I ask you to group the blocks by X. You will end up with a bunch of IGrouping<X, Block>. Make sense?
    – Sweeper
    Nov 13 '18 at 7:13






  • 1




    @pheromix Notice how whatever X is, the second argument of IGrouping is always Block, because you are grouping blocks. X is just a criteria you group by. The groups won't end up containing something else just because X is something else.
    – Sweeper
    Nov 13 '18 at 7:16






  • 1




    @pheromix In words, your code is saying this: group the commandes by both the IdentifiantClient and the Nom of the client who is joined to that commande.
    – Sweeper
    Nov 13 '18 at 7:22






  • 2




    @pheromix Yes. That's what I said in the first sentence of my first comment.
    – Sweeper
    Nov 13 '18 at 7:27
















2














Let's analyse this query expression bit by bit.



from commande in listeCommandes


It is clear that if listeCommandes is a List<Commande>, commande should be of type Commande. Right?



join
client in listeClients on commande.IdentifiantClient equals client.Identifiant


This join joins the client list to the query. Now we have a client variable of type Client. But this doesn't have much to do with commande, which is still of type Commande.



group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees


This is where it gets interesting. When you group commande, a bunch of IGrouping<AnonymousClass, Commande> are created. This is exactly the commandesGroupees here.



So what happens if you call Sum on a IGrouping<AnonymousClass, Commande>? Remember that Sum is an extension method on IEnumerable<T>, and IGrouping<AnonymousClass, Commande> implements IEnumerable<Commandes>. This means that you are basically calling Sum on a IEnumerable<Commandes>, which expects a Function<Commande, decimal>.






share|improve this answer





















  • So the first variable in the query gets the priority of the instance ?
    – pheromix
    Nov 13 '18 at 7:07






  • 1




    @pheromix In the case of group by, yes. Instead of thinking about first variable and second variable, think about this logically. I ask you to group a bunch of blocks by their color, and you will produce a bunch of groups, each of which has a bunch of blocks in it and the blocks in each group all have the same color, right? That's an IGrouping<Color, Block>. Now imagine I ask you to group the blocks by X. You will end up with a bunch of IGrouping<X, Block>. Make sense?
    – Sweeper
    Nov 13 '18 at 7:13






  • 1




    @pheromix Notice how whatever X is, the second argument of IGrouping is always Block, because you are grouping blocks. X is just a criteria you group by. The groups won't end up containing something else just because X is something else.
    – Sweeper
    Nov 13 '18 at 7:16






  • 1




    @pheromix In words, your code is saying this: group the commandes by both the IdentifiantClient and the Nom of the client who is joined to that commande.
    – Sweeper
    Nov 13 '18 at 7:22






  • 2




    @pheromix Yes. That's what I said in the first sentence of my first comment.
    – Sweeper
    Nov 13 '18 at 7:27














2












2








2






Let's analyse this query expression bit by bit.



from commande in listeCommandes


It is clear that if listeCommandes is a List<Commande>, commande should be of type Commande. Right?



join
client in listeClients on commande.IdentifiantClient equals client.Identifiant


This join joins the client list to the query. Now we have a client variable of type Client. But this doesn't have much to do with commande, which is still of type Commande.



group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees


This is where it gets interesting. When you group commande, a bunch of IGrouping<AnonymousClass, Commande> are created. This is exactly the commandesGroupees here.



So what happens if you call Sum on a IGrouping<AnonymousClass, Commande>? Remember that Sum is an extension method on IEnumerable<T>, and IGrouping<AnonymousClass, Commande> implements IEnumerable<Commandes>. This means that you are basically calling Sum on a IEnumerable<Commandes>, which expects a Function<Commande, decimal>.






share|improve this answer












Let's analyse this query expression bit by bit.



from commande in listeCommandes


It is clear that if listeCommandes is a List<Commande>, commande should be of type Commande. Right?



join
client in listeClients on commande.IdentifiantClient equals client.Identifiant


This join joins the client list to the query. Now we have a client variable of type Client. But this doesn't have much to do with commande, which is still of type Commande.



group commande by new {commande.IdentifiantClient, client.Nom}
into commandesGroupees


This is where it gets interesting. When you group commande, a bunch of IGrouping<AnonymousClass, Commande> are created. This is exactly the commandesGroupees here.



So what happens if you call Sum on a IGrouping<AnonymousClass, Commande>? Remember that Sum is an extension method on IEnumerable<T>, and IGrouping<AnonymousClass, Commande> implements IEnumerable<Commandes>. This means that you are basically calling Sum on a IEnumerable<Commandes>, which expects a Function<Commande, decimal>.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 7:01









Sweeper

64.4k1071139




64.4k1071139












  • So the first variable in the query gets the priority of the instance ?
    – pheromix
    Nov 13 '18 at 7:07






  • 1




    @pheromix In the case of group by, yes. Instead of thinking about first variable and second variable, think about this logically. I ask you to group a bunch of blocks by their color, and you will produce a bunch of groups, each of which has a bunch of blocks in it and the blocks in each group all have the same color, right? That's an IGrouping<Color, Block>. Now imagine I ask you to group the blocks by X. You will end up with a bunch of IGrouping<X, Block>. Make sense?
    – Sweeper
    Nov 13 '18 at 7:13






  • 1




    @pheromix Notice how whatever X is, the second argument of IGrouping is always Block, because you are grouping blocks. X is just a criteria you group by. The groups won't end up containing something else just because X is something else.
    – Sweeper
    Nov 13 '18 at 7:16






  • 1




    @pheromix In words, your code is saying this: group the commandes by both the IdentifiantClient and the Nom of the client who is joined to that commande.
    – Sweeper
    Nov 13 '18 at 7:22






  • 2




    @pheromix Yes. That's what I said in the first sentence of my first comment.
    – Sweeper
    Nov 13 '18 at 7:27


















  • So the first variable in the query gets the priority of the instance ?
    – pheromix
    Nov 13 '18 at 7:07






  • 1




    @pheromix In the case of group by, yes. Instead of thinking about first variable and second variable, think about this logically. I ask you to group a bunch of blocks by their color, and you will produce a bunch of groups, each of which has a bunch of blocks in it and the blocks in each group all have the same color, right? That's an IGrouping<Color, Block>. Now imagine I ask you to group the blocks by X. You will end up with a bunch of IGrouping<X, Block>. Make sense?
    – Sweeper
    Nov 13 '18 at 7:13






  • 1




    @pheromix Notice how whatever X is, the second argument of IGrouping is always Block, because you are grouping blocks. X is just a criteria you group by. The groups won't end up containing something else just because X is something else.
    – Sweeper
    Nov 13 '18 at 7:16






  • 1




    @pheromix In words, your code is saying this: group the commandes by both the IdentifiantClient and the Nom of the client who is joined to that commande.
    – Sweeper
    Nov 13 '18 at 7:22






  • 2




    @pheromix Yes. That's what I said in the first sentence of my first comment.
    – Sweeper
    Nov 13 '18 at 7:27
















So the first variable in the query gets the priority of the instance ?
– pheromix
Nov 13 '18 at 7:07




So the first variable in the query gets the priority of the instance ?
– pheromix
Nov 13 '18 at 7:07




1




1




@pheromix In the case of group by, yes. Instead of thinking about first variable and second variable, think about this logically. I ask you to group a bunch of blocks by their color, and you will produce a bunch of groups, each of which has a bunch of blocks in it and the blocks in each group all have the same color, right? That's an IGrouping<Color, Block>. Now imagine I ask you to group the blocks by X. You will end up with a bunch of IGrouping<X, Block>. Make sense?
– Sweeper
Nov 13 '18 at 7:13




@pheromix In the case of group by, yes. Instead of thinking about first variable and second variable, think about this logically. I ask you to group a bunch of blocks by their color, and you will produce a bunch of groups, each of which has a bunch of blocks in it and the blocks in each group all have the same color, right? That's an IGrouping<Color, Block>. Now imagine I ask you to group the blocks by X. You will end up with a bunch of IGrouping<X, Block>. Make sense?
– Sweeper
Nov 13 '18 at 7:13




1




1




@pheromix Notice how whatever X is, the second argument of IGrouping is always Block, because you are grouping blocks. X is just a criteria you group by. The groups won't end up containing something else just because X is something else.
– Sweeper
Nov 13 '18 at 7:16




@pheromix Notice how whatever X is, the second argument of IGrouping is always Block, because you are grouping blocks. X is just a criteria you group by. The groups won't end up containing something else just because X is something else.
– Sweeper
Nov 13 '18 at 7:16




1




1




@pheromix In words, your code is saying this: group the commandes by both the IdentifiantClient and the Nom of the client who is joined to that commande.
– Sweeper
Nov 13 '18 at 7:22




@pheromix In words, your code is saying this: group the commandes by both the IdentifiantClient and the Nom of the client who is joined to that commande.
– Sweeper
Nov 13 '18 at 7:22




2




2




@pheromix Yes. That's what I said in the first sentence of my first comment.
– Sweeper
Nov 13 '18 at 7:27




@pheromix Yes. That's what I said in the first sentence of my first comment.
– Sweeper
Nov 13 '18 at 7:27













2














Let's chunk it in smaller bits to make it simpler



from commande in listeCommandes 


commande is of type Commande



group commande by new { commande.IdentifiantClient, client.Nom } 


This is probably the bit confusing you, this still creates an IEnumerable<Commande> not an IEnumerable<anonymous<identifiantclient,nom>>, the new is what you want to group by, not what you want the grouped items to be, at this point you're creating an IGrouping that has IdentifiantClient and Nom as key but has an IEnumerable has an element



into commandesGroupees


Gives a name to the IGrouping we previously created



let total = commandesGroupees.Sum(c => c.Prix)


An IGrouping<TKey,TValue> is an IEnumerable<TValue> so as we've seen earlier each of the groups (grouped by idenfiantClient and Nom) hold a sequence of commands, since we're doing a linq query on it (sum) we're not concerned about the key but about the IEnumerable that is exposed (the IEnumerable<Command>).



This is why the sum works on Command and those have a Prix property.






share|improve this answer





















  • I am not still convinced.
    – pheromix
    Nov 13 '18 at 7:03










  • @pheromix You'll have to explain what isn't clear, that's just exactly how this works so if you don't get it you need to tell me which part isn't clear so i can better elaborate.
    – Ronan Thibaudau
    Nov 13 '18 at 7:10










  • why is it not based on the Client class ?
    – pheromix
    Nov 13 '18 at 7:12






  • 1




    @pheromix, tu as group commande, le groupe est donc un group de Commande. group client donnera un groupe de Client
    – Drag and Drop
    Nov 13 '18 at 7:15












  • So the group keyword tells what is the instance ? but not the variable in the query ?
    – pheromix
    Nov 13 '18 at 7:19
















2














Let's chunk it in smaller bits to make it simpler



from commande in listeCommandes 


commande is of type Commande



group commande by new { commande.IdentifiantClient, client.Nom } 


This is probably the bit confusing you, this still creates an IEnumerable<Commande> not an IEnumerable<anonymous<identifiantclient,nom>>, the new is what you want to group by, not what you want the grouped items to be, at this point you're creating an IGrouping that has IdentifiantClient and Nom as key but has an IEnumerable has an element



into commandesGroupees


Gives a name to the IGrouping we previously created



let total = commandesGroupees.Sum(c => c.Prix)


An IGrouping<TKey,TValue> is an IEnumerable<TValue> so as we've seen earlier each of the groups (grouped by idenfiantClient and Nom) hold a sequence of commands, since we're doing a linq query on it (sum) we're not concerned about the key but about the IEnumerable that is exposed (the IEnumerable<Command>).



This is why the sum works on Command and those have a Prix property.






share|improve this answer





















  • I am not still convinced.
    – pheromix
    Nov 13 '18 at 7:03










  • @pheromix You'll have to explain what isn't clear, that's just exactly how this works so if you don't get it you need to tell me which part isn't clear so i can better elaborate.
    – Ronan Thibaudau
    Nov 13 '18 at 7:10










  • why is it not based on the Client class ?
    – pheromix
    Nov 13 '18 at 7:12






  • 1




    @pheromix, tu as group commande, le groupe est donc un group de Commande. group client donnera un groupe de Client
    – Drag and Drop
    Nov 13 '18 at 7:15












  • So the group keyword tells what is the instance ? but not the variable in the query ?
    – pheromix
    Nov 13 '18 at 7:19














2












2








2






Let's chunk it in smaller bits to make it simpler



from commande in listeCommandes 


commande is of type Commande



group commande by new { commande.IdentifiantClient, client.Nom } 


This is probably the bit confusing you, this still creates an IEnumerable<Commande> not an IEnumerable<anonymous<identifiantclient,nom>>, the new is what you want to group by, not what you want the grouped items to be, at this point you're creating an IGrouping that has IdentifiantClient and Nom as key but has an IEnumerable has an element



into commandesGroupees


Gives a name to the IGrouping we previously created



let total = commandesGroupees.Sum(c => c.Prix)


An IGrouping<TKey,TValue> is an IEnumerable<TValue> so as we've seen earlier each of the groups (grouped by idenfiantClient and Nom) hold a sequence of commands, since we're doing a linq query on it (sum) we're not concerned about the key but about the IEnumerable that is exposed (the IEnumerable<Command>).



This is why the sum works on Command and those have a Prix property.






share|improve this answer












Let's chunk it in smaller bits to make it simpler



from commande in listeCommandes 


commande is of type Commande



group commande by new { commande.IdentifiantClient, client.Nom } 


This is probably the bit confusing you, this still creates an IEnumerable<Commande> not an IEnumerable<anonymous<identifiantclient,nom>>, the new is what you want to group by, not what you want the grouped items to be, at this point you're creating an IGrouping that has IdentifiantClient and Nom as key but has an IEnumerable has an element



into commandesGroupees


Gives a name to the IGrouping we previously created



let total = commandesGroupees.Sum(c => c.Prix)


An IGrouping<TKey,TValue> is an IEnumerable<TValue> so as we've seen earlier each of the groups (grouped by idenfiantClient and Nom) hold a sequence of commands, since we're doing a linq query on it (sum) we're not concerned about the key but about the IEnumerable that is exposed (the IEnumerable<Command>).



This is why the sum works on Command and those have a Prix property.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 6:56









Ronan Thibaudau

1,83111852




1,83111852












  • I am not still convinced.
    – pheromix
    Nov 13 '18 at 7:03










  • @pheromix You'll have to explain what isn't clear, that's just exactly how this works so if you don't get it you need to tell me which part isn't clear so i can better elaborate.
    – Ronan Thibaudau
    Nov 13 '18 at 7:10










  • why is it not based on the Client class ?
    – pheromix
    Nov 13 '18 at 7:12






  • 1




    @pheromix, tu as group commande, le groupe est donc un group de Commande. group client donnera un groupe de Client
    – Drag and Drop
    Nov 13 '18 at 7:15












  • So the group keyword tells what is the instance ? but not the variable in the query ?
    – pheromix
    Nov 13 '18 at 7:19


















  • I am not still convinced.
    – pheromix
    Nov 13 '18 at 7:03










  • @pheromix You'll have to explain what isn't clear, that's just exactly how this works so if you don't get it you need to tell me which part isn't clear so i can better elaborate.
    – Ronan Thibaudau
    Nov 13 '18 at 7:10










  • why is it not based on the Client class ?
    – pheromix
    Nov 13 '18 at 7:12






  • 1




    @pheromix, tu as group commande, le groupe est donc un group de Commande. group client donnera un groupe de Client
    – Drag and Drop
    Nov 13 '18 at 7:15












  • So the group keyword tells what is the instance ? but not the variable in the query ?
    – pheromix
    Nov 13 '18 at 7:19
















I am not still convinced.
– pheromix
Nov 13 '18 at 7:03




I am not still convinced.
– pheromix
Nov 13 '18 at 7:03












@pheromix You'll have to explain what isn't clear, that's just exactly how this works so if you don't get it you need to tell me which part isn't clear so i can better elaborate.
– Ronan Thibaudau
Nov 13 '18 at 7:10




@pheromix You'll have to explain what isn't clear, that's just exactly how this works so if you don't get it you need to tell me which part isn't clear so i can better elaborate.
– Ronan Thibaudau
Nov 13 '18 at 7:10












why is it not based on the Client class ?
– pheromix
Nov 13 '18 at 7:12




why is it not based on the Client class ?
– pheromix
Nov 13 '18 at 7:12




1




1




@pheromix, tu as group commande, le groupe est donc un group de Commande. group client donnera un groupe de Client
– Drag and Drop
Nov 13 '18 at 7:15






@pheromix, tu as group commande, le groupe est donc un group de Commande. group client donnera un groupe de Client
– Drag and Drop
Nov 13 '18 at 7:15














So the group keyword tells what is the instance ? but not the variable in the query ?
– pheromix
Nov 13 '18 at 7:19




So the group keyword tells what is the instance ? but not the variable in the query ?
– pheromix
Nov 13 '18 at 7:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274956%2flinq-to-objects-and-lambda-expression%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python