Linq on TableCellCollection - C#
void foo (TableCellCollec开发者_运维百科tion bar)
{
bar.Cast<TableCellCollection>().Where(a...
}
On the code above, the lambda 'a
' is still a TableCellCollection
rather than a TableCell
, can anyone point out what I'm doing wrong?
Thanks!
Yes, you've told it that it should be TableCellCollection
with your Cast
call. If you want to cast each element to TableCell
, that's the type argument you should give:
bar.Cast<TableCell>().Where(a...
Further to Jon's answer your code would cause a cast exception at runtime. The Cast method works on IEnumerable
collections not IEnumerable<t>
. It would act as if you were doing the below:
IEnumerable EnumerableCells = bar;
foreach (object cell in EnumerableCells)
{
TableCellCollection newCell = (TableCellCollection)cell;// this line would throw a cast exception
}
I found this useful
var eta = e.Row.Cells;
eta.Cast<TableCell>().Where(a => a.Text == "Ind_Origen").Select(a => a.Text = "Origin").FirstOrDefault();
I use Linq to query the collection looking for a string and then change it. Its usefull if you are filling a gridview and want to change the headers.
精彩评论