Solved

LINQ GroupBy

Posted on 2014-03-03
7
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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