Solved

LINQ GroupBy

Posted on 2014-03-03
7
235 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
Technology Partners: 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!

 
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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Amazon S3 .Net error 5 41
need to convert function to c# 5 43
Adding where clause to entity statement c# 1 47
Include MS Database in Winforms Project using Intellilock 5 21
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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