score:39
you could implement a custom iequalitycomparer<employee>
:
public class employee
{
public string empname { get; set; }
public string empid { get; set; }
public string emploc { get; set; }
public string emppl { get; set; }
public string empshift { get; set; }
public class comparer : iequalitycomparer<employee>
{
public bool equals(employee x, employee y)
{
return x.emploc == y.emploc
&& x.emppl == y.emppl
&& x.empshift == y.empshift;
}
public int gethashcode(employee obj)
{
unchecked // overflow is fine
{
int hash = 17;
hash = hash * 23 + (obj.emploc ?? "").gethashcode();
hash = hash * 23 + (obj.emppl ?? "").gethashcode();
hash = hash * 23 + (obj.empshift ?? "").gethashcode();
return hash;
}
}
}
}
now you can use this overload of enumerable.distinct
:
var distinct = employees.distinct(new employee.comparer());
the less reusable, robust and efficient approach, using an anonymous type:
var distinctkeys = employees.select(e => new { e.emploc, e.emppl, e.empshift })
.distinct();
var joined = from e in employees
join d in distinctkeys
on new { e.emploc, e.emppl, e.empshift } equals d
select e;
// if you want to replace the original collection
employees = joined.tolist();
score:0
try,
var newlist =
(
from x in empcollection
select new {loc = x.emploc, pl = x.emppl, shift = x.empshift}
).distinct();
score:3
i was curious about which method would be faster:
- using distinct with a custom iequalitycomparer or
- using the groupby method described by cuong le.
i found that depending on the size of the input data and the number of groups, the distinct method can be a lot more performant. (as the number of groups tends towards the number of elements in the list, distinct runs faster).
code runs in linqpad!
void main()
{
list<c> cs = new list<c>();
foreach(var i in enumerable.range(0,int16.maxvalue*1000))
{
int modvalue = int16.maxvalue; //vary this value to see how the size of groups changes performance characteristics. try 1, 5, 10, and very large numbers
int j = i%modvalue;
cs.add(new c{i = i, j = j});
}
cs.count ().dump("size of input array");
testgrouping(cs);
testdistinct(cs);
}
public void testgrouping(list<c> cs)
{
stopwatch sw = stopwatch.startnew();
sw.restart();
var groupedcount = cs.groupby (o => o.j).select(s => s.first()).count();
groupedcount.dump("num groups");
sw.elapsedmilliseconds.dump("elapsed time for using grouping");
}
public void testdistinct(list<c> cs)
{
stopwatch sw = stopwatch.startnew();
var distinctcount = cs.distinct(new ccompareronj()).count ();
distinctcount.dump("num distinct");
sw.elapsedmilliseconds.dump("elapsed time for using distinct");
}
public class c
{
public int i {get; set;}
public int j {get; set;}
}
public class ccompareronj : iequalitycomparer<c>
{
public bool equals(c x, c y)
{
return x.j.equals(y.j);
}
public int gethashcode(c obj)
{
return obj.j.gethashcode();
}
}
score:15
you can try with this code
var result = (from item in list
select new
{
emploc = item.emploc,
emppl= item.emppl,
empshift= item.empshift
})
.tolist()
.distinct();
score:42
you can use groupby
with anonymous type, and then get first
:
list.groupby(e => new {
emploc = e.emploc,
emppl = e.emppl,
empshift = e.empshift
})
.select(g => g.first());
Source: stackoverflow.com
Related Query
- Select distinct values from a list using LINQ in C#
- Select distinct values from a list using LINQ
- How to select values within a provided index range from a List using LINQ
- Not able to select values from right list using LINQ join
- Select distinct int from 2 possible fields in list using linq
- Distinct values from a list using linq
- Using a Linq query to select objects where any value of a property matches values from a list
- Using linq to select all values from one column based on a distinct second column
- How to populate a DropDownList from a SQL database using linq and only select Distinct Values
- select the distinct data from list using LINQ
- Select values from two tables using “WHERE NOT IN” and DISTINCT in LINQ in Linq to Sql
- LINQ query to return distinct field values from list of objects
- Select All distinct values in a column using LINQ
- How to select multiple values from a Dictionary using Linq as simple as possible
- Get indexes of all matching values from list using Linq
- Select Distinct List of Words from Array with LINQ
- How to get Distinct Values from List(Of T) using Linq
- Assign values from one list to another using LINQ
- How to create a comma delimited string from distinct list of values using LINQ?
- Select items from a List where the children contain the items from another List using LINQ
- Get the count of distinct elements from a list of lists using LINQ
- using linq select from list data where
- LINQ Select Last & Unique Record from a DB using List
- selecting repeated values from distinct group using LINQ
- Optimized way to select items from a Collection excluding a list of certain member using Linq
- Distinct values from a nested list using lambda
- LINQ select from table where fieldValue is in another tables list of values
- Select a matching string randomly from a list using LINQ
- Select from multiple list using LINQ
- Group and Merge multiple values from single List using LINQ
More Query from same tag
- Finding Matching Nodes in two XML files using linqpad and linq to xml is finding 0 results
- Clean way to Compare / Copy between Similar Classes
- SelectList Extension - couple expressions
- OrderBy and OrderByDescending query on a decimal stored as a string
- Get nested properties out in a list - using Index
- Update specific cell in Dataset with LINQ
- Cast<T>() with a Type variable
- Hiding hidden documents and excluding file extensions from a directory in C#
- Use LINQ to combine a property in a list of lists?
- How to iterate throgh XAttribute of XElement in C#?
- Entity framework 5.0 First or Group By Issue- After upgrading from 2.2 to 5.0
- Encapsulation and use of LINQ logic in functions using dot notation
- Deserialize SOAP XML Response
- Creating DropDownLists from the detail for a grouped ViewModel
- LINQ SQL: Left Outer Join on 2 Parameters
- Get requested column distinct values from list
- AsParallel - Before vs After in Linq Where Clause Performance
- Get child node attribute value based on parent node attribute value
- Return max repeated item in list
- Continued filtering/querying on a group by into object?
- Need for IQueryable ToArray AND ToList chained
- hat is the LINQ equivalent for the following SQL query?
- Delete DropDownList items with empty values
- Good resource to explain Lambda Expression syntax to newbie?
- How can I remove all instances of a string within a string inside a list?
- how can I Select One row where max StartDate in list objects using linq
- Order Subgrouping in Linq Query
- Filter a query by Date
- Get index in a IQueryable result
- ASP.NET MVC LINQ query