简体   繁体   中英

C# List Distinct

I have a this simple custom class for storing information:

private class ccDetails
{
    public string Raw;
    public string Head;
    public string Name;
    public string Path;
    public string Type;
}

With this I create a list using and then assign values to it from another operation. Once the assigning of values is complete I want to sort this as a distinct list based on Path.

I'm using this code:

public void main()
{
    List<ccDetailst> cclist;

   //ADD STUFF TO LIST ....
   cclist.Add(.....

   List<ccDetails> controlElements = ccList.GroupBy(x => x.Path).Select(x => x.First()).ToList();
}

I thought that this would do the trick but I get this error:

System.InvalidCastException
  HResult=0x80004002
  Message=Conversion from string "Company/Name" to type 'Boolean' is not valid.
  Source=Microsoft.CSharp
  StackTrace:
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
   at Pxxxx.frmPxxxx._Closure$__._Lambda$__7-0(ccDetails x) in C:\Users\...\frmPXXX.vb:line 73
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Pxxxx.frmPXXX.PopulateControls() in C:\Users\...\frmPXXX.vb:line 73
   at Pxxxx.frmPxxxx.Button1_Click_1(Object sender, EventArgs e) in C:\Users\...\frmPXXX.vb:line 244
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
FormatException: Input string was not in a correct format.
What am I doing wrong in the Linq expression?

Line 73 is the the Linq expression above.
I've run the following "minimal" code in LinqPad and it works fine.

void Main()
{

            List<ccDetails> cclist = new List<ccDetails>();

            for (int a = 0; a < 5; a++)
            {
                ccDetails cc = new ccDetails();
                cc.Path = $"Company/Name";
                cc.Head = "Head" + a;
                cc.Name = "Name" + a;
                cc.Type = "Type" + a;
                cc.Raw = "Raw" + a;
                
                cclist.Add(cc);
                
            }
            List<ccDetails> controlElements = cclist.GroupBy(x => x.Path).Select(x => x.First()).ToList();
            
controlElements.Dump();

}


private class ccDetails
 {
   public string Raw;
   public string Head;
   public string Name;
   public string Path;
   public string Type;
   }

For the life of me I just can't figure out where and why a Boolean conversion is being attempted. Nowhere in my code is there a Boolean variable or a cast to boolean. Any help is much appreciated.

I want to sort this as a distinct list based on Path.

This code should do the trick:

public class Program
{
    public void Main()
    {

        List<ccDetails> cclist = new List<ccDetails>();
        for (int a = 0; a < 10; a++)
        {
            ccDetails cc = new ccDetails();
            // if a is odd, then insert duplicated path for testing
            cc.Path = "Company/Name" + (a % 2 == 0 ? a.ToString() : "");
            cc.Head = "Head" + a;
            cc.Name = "Name" + a;
            cc.Type = "Type" + a;
            cc.Raw = "Raw" + a;
            cclist.Add(cc);
        }
        List<ccDetails> controlElements = cclist            
            .OrderBy(x => x.Path)
            .Distinct(new comparer())
            .ToList();
            

        foreach(var entry in controlElements)
        {
            Console.WriteLine(entry.Raw);
            Console.WriteLine(entry.Head);
            Console.WriteLine(entry.Name);
            Console.WriteLine(entry.Path);
            Console.WriteLine(entry.Type);
            Console.WriteLine("==========");
        }
    
    }
}

public class ccDetails
 {
   public string Raw;
   public string Head;
   public string Name;
   public string Path;
   public string Type;
}

public class comparer : IEqualityComparer<ccDetails>
{
    public int GetHashCode(ccDetails details)
    {
        return details.Path.GetHashCode();
    }   

    public bool Equals(ccDetails a , ccDetails b)
    {
        return a.Path.Equals(b.Path);
    }
}

And the output:

Raw1
Head1
Name1
Company/Name
Type1
==========
Raw0
Head0
Name0
Company/Name0
Type0
==========
Raw2
Head2
Name2
Company/Name2
Type2
==========
Raw4
Head4
Name4
Company/Name4
Type4
==========
Raw6
Head6
Name6
Company/Name6
Type6
==========
Raw8
Head8
Name8
Company/Name8
Type8
==========

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM