下面是我的字串陣列。我得到了這個 .Where 在線代碼,我發現它真的很有趣,我嘗試修改它以使其添加一個字符而不是洗掉一個字符。但是,我在最后一行的“ indexremove ”變數下不斷收到“ Method Name Expected 錯誤。這個問題有什么解決方案嗎?或者我的修改無法作業?
string[] TestArray = {"a", "b", "c", "d", "e"};
int indexremove = 0; // for removing the character in the 1st position; a.
TestArray = TestArray.Where((source, index) => index != indexremove).ToArray(); //source, index points to the array's character collection, and index != indexremove causes A to be removed from the index of the array. Then it is converted to an array.
foreach (string value in TestArray) {Console.WriteLine(value); //displays output of new array without "a" }
string indextoadd = "a"; //trying to re-add the character back in to the array
TestArray = TestArray.Where((source, index) => index indexremove(indextoadd)).ToArray(); //Method Name Expected on indexremove
uj5u.com熱心網友回復:
Where不能這樣作業。它是一種過濾設備,因此它本質上不能用于擴展/添加到事物串列中。如果某些測驗回傳,您可以對 Where 做的唯一事情是防止輸出其中一個輸入項false
對于任何給定的專案串列;
string[] testArray = {"a", "b", "c", "d", "e"};
您可以撰寫一個Where, 并傳遞給Where一小段代碼(我們傾向于將其稱為 lambda),這就像一個無名的方法。它必須接受一個或兩個引數,并且必須回傳一個布林值。Where將遍歷輸入的專案串列,呼叫您在每個專案上傳遞的 lambda。如果 lambda 回傳 true,則輸出輸入的專案。如果回傳false,則不輸出被回圈的串列項
這些是概念上的等價物:
//this nice short thing
testArray.Where(s => s.Length == 1);
//is like this longer thing
public bool LengthIs1(string s){
return s.Length == 1;
}
...
foreach(var s in testArray)
if(LengthIs1(s))
output s; //this is a pretend thing: `output` doesn't exist but it means I don't also have to explain yield return. Just imagine that it sends the value out and carries on looping
例子
testArray.Where(s => true); //every item will be output
testArray.Where(s => false); //no item will be output
testArray.Where(s => s == "a"); //only items equal to "a" will be output
testArray.Where(s => s.StartsWith("b")); //only items starting with b will be output
testArray.Where(s => DateTime.Now.DayOfWeek == DayOfWeek.Tuesday); //every item will be output if it's Tuesday, otherwise no item will be output
這些示例僅將一個引數s傳遞給 lambda。還有另一種形式,即您正在使用的形式,其中 lambda 可以有兩個引數 - 第一個引數再次是陣列中的專案。第二個引數是陣列中item的索引
testArray.Where((s,index) => true); //every item will be output
testArray.Where((s,index) => index == 0); //only the first item will be output
testArray.Where((s,index) => index != 1); //everything apart from the second item will be output
testArray.Where((s,index) => index%2 == 0); //even indexed items will be output
如您所見,沒有任何擴展或添加更多專案的空間。您從 100 個專案開始,您只能輸出 0 到 100 個專案之間的某個位置
在您發布的代碼中,第一項被過濾掉了,因為 lambda 評估了要洗掉的索引與要洗掉的索引,并且在它們不相同的所有情況下都將回傳 true(保留),并在它們相同時回傳 false(洗掉)
如果要輸出更多專案,則必須使用其他內容,例如 Concat
new[]{1,2}.Concat(new[]{3,4})
這將輸出 1,2,3,4 的列舉
因為Where產生了一個可列舉的東西,并且Concat可以在一個可列舉的東西上呼叫并接受另一個可列舉的東西作為引數,這意味著我們可以做類似的事情:
testArray
.Where((s,index) => index != indextoremove)
.Concat(
testArray.Where((s,index) => index == indextoremove)
)
第一個Where僅排除indextoremove(保留其余部分),第二個Where保留僅保留indextoremove(排除其余部分)。這Concat將有效地采用第一個序列,其中“a”已被排除,然后連接到第二個序列,其中“a”是唯一包含的序列。這實際上會將“a”從序列的開頭移動到序列的結尾
這是非常低效的,但這個例子更多的是關于學習
(source, index) => index indexremove(indextoadd)
唉,這是一個語法錯誤。您已經創建了 indextoadd 一個字串,而要洗掉的 index 是一個 int。你不能在一個 int 變數名后面加上一個 ( 因為你不能執行/呼叫一個 int。你也不能執行一個將字串傳遞給它的 int。作為 lambda 棺材中的最后一顆釘子,它應該回傳一個 bool 但是如果 int int(string) 甚至可能的話,你已經做了一些接近數字結果的事情。在完整的方法術語中,你做了類似的事情:
public bool AMethod(string source, int index){
string indextoadd = "a";
int indexremove = 1;
return index indexremove(indextoadd);
}
希望你能看到有很多語法和邏輯問題..
uj5u.com熱心網友回復:
using System;
using System.Linq;
namespace Problems
{
class Program
{
static void Main(string[] args)
{
string[] TestArray = { "a", "b", "c", "d", "e" };
int indexremove = 1;
var RemovedIndex = TestArray.Skip(indexremove);
foreach(var t in RemovedIndex)
{
Console.WriteLine(t);
}
RemovedIndex.ToList().Insert(indexremove, "a");
RemovedIndex = TestArray.ToArray();
foreach (var t in RemovedIndex)
{
Console.WriteLine(t);
}
}
}
}
由于陣列是固定的,我選擇跳過陣列中的第一個索引。列印第一個控制臺以查看是否跳過了第一個字符。
然后,由于您需要回傳字符,因此我將當前陣列轉換為串列并將字符插入回去。請記住之后將其轉換回陣列。
通過運行第二個回圈查看結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/413815.html
標籤:
