簡體   English   中英

C# 列表不同

[英]C# List Distinct

我有一個簡單的自定義 class 用於存儲信息:

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

有了這個,我使用創建一個列表,然后從另一個操作中為其分配值。 一旦值的分配完成,我想將其排序為基於路徑的不同列表。

我正在使用這段代碼:

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();
}

我認為這可以解決問題,但我收到了這個錯誤:

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?

第 73 行是上面的 Linq 表達式。
我在 LinqPad 中運行了以下“最小”代碼,它工作正常。

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;
   }

對於我的一生,我只是不知道在哪里以及為什么要嘗試 Boolean 轉換。 我的代碼中沒有一個 Boolean 變量或轉換為 boolean。 任何幫助深表感謝。

我想根據路徑將其排序為不同的列表。

這段代碼應該可以解決問題:

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);
    }
}

和 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
==========

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM