 |
|
 |
Right. Most remember Linq offers the ToDictionary, but not many remember the ToLookUp.
I propose to go with the following:
var result = src.ToLookup(s => s.TheKey)
.Select(res => res.First())
.ToList();
|
|
|
|
 |
|
 |
This sounds good now that .NET 4 exists. Great recommendation.
Stephen Inglish
Consultant
Sogeti USA
MCPD: Windows Developer
MCPD: Web Developer
MCPD: Enterprise Developer
|
|
|
|
 |
|
 |
Functionality already exists
|
|
|
|
 |
|
 |
Prior to 2010-04-12, .NET 4 didn't exist, LINQ was just released, and I couldn't find it elsewhere.
To be helpful, instead of just blasting this as useless, link what functionality you are stating already exists so others who find this article can use it.
Stephen Inglish
Consultant
Sogeti USA
MCPD: Windows Developer
MCPD: Web Developer
MCPD: Enterprise Developer
|
|
|
|
 |
|
 |
Ignoring the fact that you can to this using other means, there's one thing that sticks out about your imlementation, which is ordering.
List is an ordered list. Hence, the relative order of elements should be preserved in operations like this. Your implementation uses the order of the dictionary keys. If you insist on rolling your own, use the dictionary (or a HashSet to store keys, but built the result from the input list), you can preserve the order like this:
HashSet<t> keys = new HashSet<t>();
List<t> result = new List<t>(inputList.Count);
foreach( T item in list )
{
KEY key = func(item);
if( keys.Add( key ) )
result.Add(item);
}
resultList.TrimExcess();
/pre> </t></t></t></t>
|
|
|
|
 |
|
 |
Three times I tried to post a snippet that uses generics, but the editor refuses to keep the code intact.
It's simply astounding that a web site devoted to programming can be this disfynctional.
|
|
|
|
 |
|
 |
tonyt wrote: It's simply astounding that a web site devoted to programming can be this disfynctional.
What, exactly, is dysfunctional? I can show generics perfectly:
List<MyType> myList = new List<MyType>();
|
|
|
|
 |
|
 |
Had you bothered to eloaborate on how that's done, I would have considered your comment to be constructive.
|
|
|
|
 |
|
 |
Well, it is HTML you're typing in there. The normal rules of HTML apply.
Therefore to get a < you type <
> is >
& is &
My comment was more aimed at your predisposition to blame others for your failure to understand. Instead of seeking a solution you chose to blame others. Had you sought a solution without blaming others I'd have considered your comment to warrant a solution.
|
|
|
|
 |
|
 |
Feel free to misrepresent my comments in any manner you wish.
But the fact of the matter is, that I most certainly am blaming others for a problem that should not exist, to begin with.
The fact that you choose to be an apologist for party responsible for the problem, doesn't change the facts, it only affects how you choose to misrepresent them.
|
|
|
|
 |
|
 |
I had the same problem. The workaround ist to post the message then open it for editing and update the text which is now HTML-encoded.
You're right that on codeproject this shouldn't be an issue.
Cheers
Günther
|
|
|
|
 |
|
 |
Thanks, I'll try that next time.
|
|
|
|
 |
|
 |
If you are talking about writing < and > when posting messages, there is at least in my browser a formatting row below the message text field. The formatting row contains "buttons" for, among some other things, inserting < and > in the message text. As has already been mentioned, < is the HTML character entity for the less-than sign and > is the HTML character entity for the greater-than sign, so pressing the buttons will insert the corresponding text in the message. There is also a check box, "Ignore HTML tags in this message (good for code snippets)" that can be checked. If checked, you can write < and > as normal text, but cannot format the message.
|
|
|
|
 |
|
|
 |
|
 |
I think you might have misinterpreted the meaning of 'ordered'.
'Ordered' means there is a contract stipulating that elements appear the order in which they were added.
'Unordered' means that there is no contract WRT the ordering of elements (e.g., HashSet<T>, Dictionary<TKey,TValue>, etc).
modified on Saturday, December 6, 2008 9:49 PM
|
|
|
|
 |
|
 |
I had a language mismatch - Ordered vs Sorted (in my mother's language it's the same )
When reading the List<T>-article on MSDN there's no point saying the list isn't ordered. Furthermore the terms 'sort' and 'order' are used for the same -> sorting.
It is stated that the List<T> isn't sorted by default - no comment for order.
Cheers
Günther
|
|
|
|
 |
|
|
 |
|
 |
Hi,
when using Distinct you have to provide a IEqualityComparer<T>. With this extension you can define a lambda-expression, it's in my opinion easier to use.
Intersect? Can't imagine to use it for this task.
Cheers
Günther
|
|
|
|
 |
|
 |
I was expecting the author to repond.
I have used Intersect for similar tasks, finding unique rows in a DataTable
public static DataTable SelectDistinct(this DataTable dt, string columnName)
{
int index = dt.Columns[columnName].Ordinal;
Dictionary<object, DataRow> set = new Dictionary<object, DataRow>();
foreach(DataRow row in dt.Rows)
{
if(!set.ContainsKey(row.ItemArray[index]))
set.Add(row.ItemArray[index], row);
}
var rows = dt.AsEnumerable().Intersect(set.Values);
DataTable newDataTable = dt.Clone();
foreach(DataRow row in rows)
{
newDataTable.Rows.Add(row.ItemArray);
}
return newDataTable;
}
only two letters away from being an asset
|
|
|
|
 |
|
 |
OK, you intersect the (unique) keys with the original list to return the unique list. Nice!
Cheers
Günther
|
|
|
|
 |
|
 |
Hi, cool idea!
When I understood the purpose of this extension correctly than it can be rewritten to:
public static class gfoidlLINQExtension
{
public static List<t> Unique<K,T>(this List<t> inputList, Func<T,K> func)
{
#region Input validation
if (func == null)
throw new ArgumentNullException("Key selector function cannot be null");
if (inputList == null)
return null;
if (inputList.Count == 0)
return new List<t>(); #endregion
var grp = inputList.GroupBy(func);
return grp.Select(g => g.First()).ToList();
}
}
</t></t></t>
Cheers
Günther
modified on Friday, December 5, 2008 6:05 PM
|
|
|
|
 |
|
 |
Nice to see the power of Linq here.. I wonder if Stephens implementation is more memory efficient?. Also a few comments on both versions:
- you can use IList instead of List
- when the input list has zero entries you shouldn't return the original reference, but a new empty list. Reason: suppose that someone starts filling the original list after the Unique call.. now the 'Unique' result magically has entries too.
|
|
|
|
 |
|
 |
Instead of just extending List (this List), extend IEnumerable (this IEnumerable<t> ) instead so that you can also do this on arrays (useful for DataSets since they do not store things in List<t>).
public static IEnumerable<T> Unique<KEY, T>(this IEnumerable<T> enumerableList, Func<T, KEY> func)
It still works with List this way.
|
|
|
|
 |
|
 |
As mentioned here before, Linq already have support for this, so this extension is useless.
The following line of code will even work with Linq to Sql:
tblUsers.GroupBy(u => u.fkCultureID).Select(u => u.First());
Magnus Persson
|
|
|
|
 |