2011-05-06 15 views
0
void foo (TableCellCollection bar) 
{ 
    bar.Cast<TableCellCollection>().Where(a... 
} 

在上面的代碼中,拉姆達「a」仍然是一個TableCellCollection而非TableCell,任何人都可以指出我在做什麼錯? 謝謝!的LINQ上TableCellCollection - C#

回答

4

是的,你有告訴它應該是TableCellCollection與你的Cast電話。如果你想每個元素轉換爲TableCell,這類型的參數,你應該給:

bar.Cast<TableCell>().Where(a... 
1

繼Jon的回答您的代碼在運行時將導致轉換異常。 Cast方法適用於IEnumerable集合而非IEnumerable<t>。因爲如果你是做在它下面將採取行動:

IEnumerable EnumerableCells = bar; 

foreach (object cell in EnumerableCells) 
{ 
    TableCellCollection newCell = (TableCellCollection)cell;// this line would throw a cast exception 
} 
0

我發現這個有用的

var eta = e.Row.Cells; 

eta.Cast<TableCell>().Where(a => a.Text == "Ind_Origen").Select(a => a.Text = "Origin").FirstOrDefault(); 

我使用LINQ查詢集合尋找一個字符串,然後改變它。它是有用的,如果你正在填寫一個gridview並想更改標題。