dchau12
asked on
LINQ question
I have a List object in c#. I want to group by three fields in that list and test for a duplicate record. In SQL, it would look like this:
select field1, field2, field3
from Table1
group by field1, field2, field3
having count(*) > 1
Is this possible to do in LINQ?
select field1, field2, field3
from Table1
group by field1, field2, field3
having count(*) > 1
Is this possible to do in LINQ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My example is VERY crude--don't read into the variable names, I merely wanted to demonstrate syntax.
ASKER
I got it working, and I can see the value that it found, but how do I then mine that value out of "data"? intelisense only brings up more predicate methods.
ASKER
I can see everything in data2's method "Results View", but how do i access the method?
var data2 = from f in myList
group f by new { f.TrpCarrierId, f.Mode, f.CarrierStatusCode }
into myGroup
where myGroup.Count() > 1
select new
{
myGroup.Key.TrpCarrierId,
myGroup.Key.Mode,
myGroup.Key.CarrierStatusC ode,
MyCount = myGroup.Count()
};
var data2 = from f in myList
group f by new { f.TrpCarrierId, f.Mode, f.CarrierStatusCode }
into myGroup
where myGroup.Count() > 1
select new
{
myGroup.Key.TrpCarrierId,
myGroup.Key.Mode,
myGroup.Key.CarrierStatusC
MyCount = myGroup.Count()
};
I believe it would be something like:
foreach (var d in data2)
{
string.Concat(d.TrpCarrier Id, d.Mode); // etc.
}
foreach (var d in data2)
{
string.Concat(d.TrpCarrier
}
ASKER
You rock!
:)
Glad to help.
Glad to help.