我正在閱讀這個https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.find?view=net-6.0并嘗試將其作為示例代碼:
using System;
using System.Collections.Generic;
// Simple business object. A PartId is used to identify a part
// but the part name can change.
public class Part : IEquatable<Part>
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " PartId " Name: " PartName;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
Part objAsPart = obj as Part;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
public override int GetHashCode()
{
return PartId;
}
public bool Equals(Part other)
{
if (other == null) return false;
return (this.PartId.Equals(other.PartId));
}
// Should also override == and != operators.
}
public class Example
{
public static void Main()
{
// Create a list of parts.
List<Part> parts = new List<Part>();
// Add parts to the list.
parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });
parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ;
// Write out the parts in the list. This will call the overridden ToString method
// in the Part class.
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
// Check the list for part #1734. This calls the IEquatable.Equals method
// of the Part class, which checks the PartId for equality.
Console.WriteLine("\nContains: Part with Id=1734: {0}",
parts.Contains(new Part { PartId = 1734, PartName = "" }));
// Find items where name contains "seat".
Console.WriteLine("\nFind: Part where name contains \"seat\": {0}",
parts.Find(x => x.PartName.Contains("seat")));
// Check if an item with Id 1444 exists.
Console.WriteLine("\nExists: Part with Id=1444: {0}",
parts.Exists(x => x.PartId == 1444));
/*This code example produces the following output:
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
ID: 1634 Name: shift lever
Contains: Part with Id=1734: False
Find: Part where name contains "seat": ID: 1434 Name: regular seat
Exists: Part with Id=1444: True
*/
}
}
現在我想知道是否有辦法將特殊行分配到文本框或類似的東西中?對不起,我的英語不好,我會舉個例子。在這部分代碼中,我們有
parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });
parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ;
現在我想將 PartName 分配到 ID 為 1634 的文本框或字串中,有人可以告訴我在哪里可以得到類似的東西嗎?在這一行
// Find items where name contains "seat".
Console.WriteLine("\nFind: Part where name contains \"seat\": {0}",
parts.Find(x => x.PartName.Contains("seat")));
它將檢查零件名稱是否包含“座位”,并且輸出為
ID:1434 名稱:普通座
問題是我不想要整個部分,我只想要 PartName 或 PartID 像:“1434”,只有這個。甚至沒有“ID:1434”。我希望你們理解我,我盡力了:-(再次抱歉我的英語不好。謝謝
uj5u.com熱心網友回復:
目前 Find 回傳整個物件,默認情況下呼叫 ToString() 以將其顯示為字串值。
如果您只想PartId顯示 ,則可以執行以下操作:
Console.WriteLine("\nFind: Part where name contains \"seat\": {0}",
parts.Find(x => x.PartName.Contains("seat"))?.PartId );
您可以改為使用PartName或 的任何其他屬性Part
如果你想要更復雜的東西,那么你可以在 Part 類中創建一個新方法并呼叫它。
public class Part : IEquatable<Part>
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " PartId " Name: " PartName;
}
public string AlternativeToString()
{
return this.PartId " " this.PartName;
}
//rest stays the same
}
并像這樣稱呼它
Console.WriteLine("\nFind: Part where name contains \"seat\": {0}",
parts.Find(x => x.PartName.Contains("seat"))?.AlternativeToString() );
uj5u.com熱心網友回復:
使用 linq:
var myPartName = parts.Where(p => p.PartId == 1432).Select(p => p.PartName).FirstOrDefault();
這將回傳具有給定 ID 的第一個 partName,如果不存在,則回傳 null。
您還可以考慮將串列轉換為以 partId 為鍵的字典,這樣可以更快更輕松地查找:
var partDict = parts.ToDictionary(p => p.PartId, p => p);
var myPartName = parts[1432].PartName; // will throw if id does not exist, use TryGet for a safer version
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/366868.html
