e-Beng
e-Beng

Reputation: 11

Using LinQ with group by more than one column

I'm Newbi in LinQ, I have problem with group by in linQ.
I wan to query like this:

    select
    MAX(TCheckpointGrouping.Id) AS CheckpointGroupingId,
    MAX(TCheckpointGrouping.MCheckpointId) AS CheckpointId,
    MAX(MCheckpoint.Name) AS CheckpointName,
    MAX(CAST(MCheckpoint.IsMajor AS VARCHAR)) AS IsMajor,
    MAX(TCheckpointGrouping.MIndicatorId) AS IndicatorId,
    MAX(MIndicator.Name) AS IndicatorName,
    MAX(MCriteria.Id) AS CriteriaId,
    MAX(MCriteria.Name) AS CriteriaName,
    MAX(MPrinciple.Id) AS PrincipleId,
    MAX(MPrinciple.Name) AS PrincipleName,
    MAX(TCheckpointGrouping.RelationToCheckPoint) AS RelationToCheckPoint
    from TCheckpointGrouping
    inner join MCheckpoint on MCheckpoint.Id = TCheckpointGrouping.MCheckpointId
    inner join MIndicator on MIndicator.Id = TCheckpointGrouping.MIndicatorId
    inner join MCriteria on MCriteria.Id = MIndicator.MCriteriaId
    inner join MPrinciple on MPrinciple.Id = MCriteria.MPrincipleId
    group by
    TCheckpointGrouping.MCheckpointId,
    TCheckpointGrouping.MIndicatorId

How can i convert query above into LinQ (VB.NET)

thanks bestRegards

Upvotes: 0

Views: 649

Answers (2)

Abhijeet Zade
Abhijeet Zade

Reputation: 26

I am not sure about this, but you can try it. In select part i have not included all the columns.

var result= from TChkgp in TCheckpointGrouping
        join MCpoint in  MCheckpoint on  TChkgp.Id equals MCpoint.Id
    join MIndtor in MIndicator on TChkgp.MIndicatorId equals MIndtor.Id
    join MCrteia in MCriteria on MIndtor.Id equals MIndtor.MCriteriaId
    join MPrncple in MPrinciple on MCrteia.MPrincipleId equals MPrncple.Id
    group TChkgp by new (TChkgp.MCheckpointId,TChkgp.MIndicatorId} into g 
    select new {
    CheckpointGroupingId =TChkgp.Id.Max(),
    CheckpointId =TChkgp.MCheckpointId.Max,
    ....
    ....


    };

you can see one simple example on following link

Group and sum in linq

Upvotes: 0

J.Hudler
J.Hudler

Reputation: 1258

I'm tempted to convert this SQL query to LINQ for you, but I think that would be a waste of opportunity for you to learn yourself.

There's a great page from Microsoft with lot of VB.NET Linq situations: 101 Linq Samples.

You can even find an example of a Group By using Multiple Columns.

Good learning. :)

Upvotes: 2

Related Questions