Home > Net >  Emergency!!!!! Cycle judgment, remove the duplicate data in the list in another list
Emergency!!!!! Cycle judgment, remove the duplicate data in the list in another list

Time:01-30

Cycle judgment, remove the duplicate data in the list on another list,

CodePudding user response:

Want to see the list type is an object or a single data type

A single data types directly compare whether repeated

Object, the need to write another compare

CodePudding user response:

 

Public static class ObjectExtensions
{
Public static IEnumerable DistinctBy (this IEnumerable The source, Func KeySelector)
{
HashSet SeenKeys=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 sweet

CodePudding user response:

 List List=new List () {1, 2, 3, 12, 2, 3}; 
List NewList=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 data

CodePudding 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};
Dictionary Dic=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:

refer to 7th floor ziqi0716 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};
Dictionary Dic=new Dictionary (a);
Foreach (var I in the list)
{
Dic [I]=I;
}
Var newList=dic. Keys;
Foreach (var I in newList)
{
Console. WriteLine (I);
}

The seventh line can be output into List : var newList=dic. Keys. ToList ();

CodePudding user response:

Thank you, I want to ask is this list_ List Type judgment contained can determine whether include UserKey
If List How do I know if does it include UserKey?
  •  Tags:  
  • C#
  • Related