CodePudding user response:
Want to see the list type is an object or a single data typeA single data types directly compare whether repeated
Object, the need to write another compare
CodePudding user response:
Public static class ObjectExtensions
{
Public static IEnumerableDistinctBy (this IEnumerable The source, Func KeySelector)
{
HashSetSeenKeys=new HashSet (a);
Foreach (TSource element in the source)
{
If (seenKeys. Add (keySelector (element)))
{
Yield return element;
}
}
}
}
///& lt; Summary>
///return subject data
///& lt;/summary>
[Serializable]
Public class QuestionResult
{
///& lt; Summary>
///no.
///& lt;/summary>
Public long Id {get; set; }
///& lt; Summary>
///the topic content
///& lt;/summary>
Public string Title {get; set; }
}
List. DistinctBy (e=& gt; New} {e.I d) ToList ();
CodePudding user response:
Write this do not extend his sweetCodePudding user response:
ListList=new List () {1, 2, 3, 12, 2, 3};
ListNewList=new List (a);
//to heavy
//var NewList=List. The Distinct ();
//loop to heavy
The foreach (var item in the List)
{
if (! NewList. The Contains (item))
{
NewList. Add (item);
}
}
CodePudding user response:
For a list. Select (p=& gt; P.n ame). Distinct (.) ToList ();//remove the name repeated list dataCodePudding user response:
This is a basic problem, need to first understand what is repetitive data, the processing of a reference type and value type is not the same, a reference type as long as the same value that is repeated, reference type must be the same object reference is repeated, see==and Equals the relation and distinction between, get this understood, the other is right,CodePudding user response:
Change the version of the dictionary:
Var list=new List() {1, 1, 2, 2, 2, 5, 8, 5, 5, 8, 5, 5, 48, 85, 5, 54, 85};
DictionaryDic=new Dictionary (a);
Foreach (var I in the list)
{
Dic [I]=I;
}
Var newList=dic. Keys;
Foreach (var I in newList)
{
Console. WriteLine (I);
}
CodePudding user response: