Solved

LINQ GroupBy

Posted on 2014-03-03
7
233 Views
Last Modified: 2016-02-10
Hi Experts,

I need your help to add grouping by T.tid   in this code. Thank you in advance.


                    var mydata = db.data
                   
                    .Where(T => T.some.Any(F => F.my_id == another_id))
                   
                    .ToArray()
                 
                    .Select(T => new MyInfo
                    {
                        TId = T.tid,
                        PData = T.data
                       
                     
                    }).ToArray();
0
Comment
Question by:fpoyavo
  • 4
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39902348
Hi fpoyavo;

What is the data type of T.tid? Because if you create a grouping it will return a hierarchical data object. What is also the data type of MyInfo.Tid? Because it will need to store that object.

Can you show in a diagram what you want that grouping to look like or what you need it to look like.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39903027
T.tid is int. MyInfo.Tid is int. MyInfo.PData is string.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39903069
So what are you trying to accomplish by grouping in the query?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:fpoyavo
ID: 39905456
To eliminate duplicated data
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39905468
Grouping will not get rid of duplicates. When you say duplicates does that mean that all columns having the same values or certain columns only.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39905554
Some of them. Not all.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39907759
Hi fpoyavo;

The IEqualityComparer below is used to see if two rows in the database table can be considered duplicates. The IEqualityComparer is only considering two columns to test whether they are considered duplicates or not. If more columns are needed to determine  if they are duplicate then you must add those columns as well.

// See comment below
var ec = new OrderEqualityComparer();

// In the query you need to add the Distinct method after the first ToArray.
var mydata = db.data
               .Where(T => T.some.Any(F => F.my_id == another_id))
               .ToArray()
               .Distinct(ec)
               .Select(T => new MyInfo
               {
                   TId = T.tid,
                   PData = T.data              
               }).ToArray();

// This class is used to match two data object from the database table
// data to see if they are considered duplicates.               
class OrderEqualityComparer : IEqualityComparer<data>
{

    public bool Equals(data d1, data d2)
    {
        if (d1.TID == d2.TID & d1.OtherCoulmn == d2.OtherCoulmn)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public int GetHashCode(data d)
    {
        int hCode = d.TID.GetHashCode() ^ d.OtherCoulmn.GetHashCode();
        return hCode.GetHashCode();
    }

}

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Set value of dynamically created checkboxlist 2 34
Help with exporting to excel 4 27
Expression Evaluater 3 25
JSON  parse help 7 26
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question