我從 csv 檔案匯入資料,然后將其存盤在串列 ( List<Customer> customers) 中。通過創建和添加物件將資料添加到串列中。物件是使用將檔案內容作為引數的建構式創建的,一切都是字串。據我了解,該串列屬于客戶型別。我想做一個 if 陳述句來檢查串列中是否存在用戶輸入(字串)。
我試過這段代碼:
ImportData("CustomerData.csv");
Console.WriteLine("Please provide customer name:");
string customer_Name = Console.ReadLine();
//if (customers.Contains(customer_Name))
bool b = customers.Contains(customer_Name);
//if (customers.Any(customers.Contains(customer_Name)))
if (b)
沒有任何效果,我收到錯誤 CS1503 Argument 1: cannot convert from 'string' to 'MyNameSpace.Customer'。我真的不明白這是什么意思。我想也許是因為串列是<Customer>型別的?我還有一個名為 Customer 的課程。我試圖將串列型別更改為<string>,但沒有幫助。
uj5u.com熱心網友回復:
List.Contains要求您提供客戶物件。由于您可能沒有覆寫 Equals 和 GetHashcode 以允許將兩個不同的客戶實體視為相等,因此使用它可能毫無意義,因為它需要您在串列中找到您想要的客戶實體,然后詢問串列如果它包含您找到的實體(您已經知道)
IEnumerable.Any要求您提供對客戶物件進行操作并回傳 true 或 false 的方法或小函式(通常稱為 lambda)。這對您的情況更有用
簡短的形式如下:
if (customers.Any(c => c.CustomerName == customer_Name))
這c => c.CustomerName == customer_Name是一個迷你方法(lambda):
c方法的引數;隱含的客戶(因此我將其命名為c),原因我稍后會介紹=>引數和正文之間的分隔符;C# 用它來理解你正在制作一個 lambdac.CustomerName == customer_Name決議為布林值的陳述句
在長格式中,它可能看起來像:
bool SomeMethod(Customer c){
return c.CustomerName == customer_Name;
}
我們可以將這種長形式縮短為短形式,因為編譯器可以計算出很多東西:
- 規則是 Any 需要回傳布林值的東西,所以我們不需要
bool. - 這意味著輸入引數
c是 aCustomer,因為 List 中充滿了Customers。 - 我們可以放棄,
{ return ... }因為 C# 有運算式體;“左邊的引數,然后是 a=>,然后是決議為我們首先提到的回傳值型別的單個陳述句”意味著我們可以使用大括號和return關鍵字跳過 - 在撰寫 lamdba 時,我們不需要給它命名,因為我們從不在任何地方通過名稱來參考它;它純粹體現在它的定義中,并且只被我們賦予它的一件事所使用,因此它可以保持無名
所以我們的長形式逐漸歸結為:
//long form: a class level method
bool SomeMethod(Customer c){
return c.CustomerName == customer_Name;
}
//shorter: expression body; this is still valid C# to put in a class and you could write all your "one line return" methods like this
bool SomeMethod(Customer c) => c.CustomerName == customer_Name;
//shortest: a lambda, this has to be stored in a variable or used in a method call that accepts a lambda - you cannot write this in a class level statemetn because the compiler needs some surrounding context to figure bits of it out
c => c.CustomerName == customer_Name
如果您查看檔案,Any它會告訴您必須提供的內容:

以下是如何破譯這個:
Any是一種可以在IEnumerable. List 是IEnumerable,所以Any可以在 List 上呼叫。List 充滿了具有特定型別的物件,TSource. TSource將被串列中的任何內容替換,在這種情況下Customer為您。這個詞this意味著它是一種擴展方法;一個技巧,這意味著您可以直接在客戶串列上呼叫它:
//you could do this; call Any and pass customers in
Enumerable.Any(customers, ...)
//but its more normal, readable etc to do this:
customers.Any(...)
在第一個引數上的使用this允許您像后者一樣撰寫,編譯器會將其理解為前者。它可以讓你“讓它看起來像是你在呼叫它的東西上添加了一個方法,即使你沒有”
更有趣的部分是Func<TSource,bool>爭論。這意味著“一個接受 aTSource并回傳 a的函式bool”。請記住,TSource您的List<Customer>意思是“客戶”。
最后提到的是回傳型別,其他一切都是方法的引數。如果您看到Funct<string, int, TSource, DateTime>它的意思是“具有 3 個輸入引數的函式或方法;astring和(Customer) 按順序排列,并且必須回傳 DateTime” int。TSource允許提供與此簽名者匹配的任何方法。
回圈回到Funct<TSource, bool>for Any; 您必須提供一個方法,該方法需要一個TSource(客戶,在您的情況下)并回傳一個bool.
這符合:
private string customer_Name = "john" //class level variable
//method that matches the rule "takes a Customer, returns a bool"
bool SomeMethod(Customer c){
return c.CustomerName == customer_Name;
}
//can be passed to list's Any
customers.Any(SomeMethod)
這種較短的形式也符合以下條件:
customers.Any(c => c.CustomerName == customer_Name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
also a method that "takes a customer, returns a bool"
Any將采用您給它的方法并開始回圈串列,呼叫該方法并傳入它正在回圈的當前客戶。一旦它得到一個真,它就會停止并回傳真,因為它發現“任何客戶匹配......”是真的
我收到錯誤 CS1503 Argument 1: cannot convert from 'string' to 'MyNameSpace.Customer'。我真的不明白這是什么意思
希望現在您可以看到,如果您這樣做:
customers.Any("John")
customers.Any需要您提供“一種接受客戶并回傳布林值的方法”,又名Func<Customer,bool>- 您不能提供像“John”這樣的字串,因為字串不是Func<Customer,bool>..
..想想吧。如果您確實提供了像“John”這樣的字串 - C# 如何知道這是您要查找的名稱,而不是地址?
提供一個方法作為引數,而不是提供資料作為引數,這可能是讓你頭腦清醒的最重要的事情。為什么微軟撰寫接受方法而不是資料的代碼?因為簡單地說,他們不可能為人們如何使用 List 的所有變體撰寫代碼,所以他們只是說“我們將回圈串列,呼叫一些我們無法想象的代碼——你提供的代碼——如果你的代碼回傳真,我們將回傳真”
微軟撰寫 C# 是很自然的,但缺少一些只有你才能知道的部分 - 因此為什么“像資料一樣傳遞方法”至關重要
uj5u.com熱心網友回復:
customers是List<T>where T is Customer,您的錯誤是告訴您它無法將 a 轉換string為型別別Customer,我認為您想要Linq用來查找Customerxxx (客戶名稱)的屬性等于用戶輸入的任何物件,您可以這樣做這個,你會somestringproperty用你的類屬性名稱替換:
bool b = customers.Any(a => a.somestringproperty == customer_Name);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/475462.html
上一篇:在Clojure中,將帶有前導0的數字傳遞給str會導致奇怪的行為。這是什么功能?
下一篇:資料集中值的Python總和
