Solved

LINQ GroupBy

Posted on 2014-03-03
7
234 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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