Solved

LINQ GroupBy

Posted on 2014-03-03
7
230 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 62

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 62

Expert Comment

by:Fernando Soto
ID: 39903069
So what are you trying to accomplish by grouping in the query?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

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

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 62

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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!
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now