Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cannot type arguments be inferred from this simple code?

Tags:

linq

c#-3.0

Using .NET 3.5, but the Select call gives the error. Shouldn't the compiler be clever enough to infer this? If not, why not?

public IEnumerable<Customer> TableToCustomers(Table table)
{
    return table.Rows.Select(RowToCustomer);
}

private Customer RowToCustomer(TableRow row)
{
    return new Customer { ... };
}
like image 606
ciscoheat Avatar asked Dec 05 '25 18:12

ciscoheat


1 Answers

The Rows property is defined as TableRowCollection Rows {get;}

public sealed class TableRowCollection : IList, ICollection, IEnumerable

It is not IEnumerable<TableRow>, so it is just IEnumerable, therefore it cannot infer the type as being TableRow.

You can do this instead:

public IEnumerable<Customer> TableToCustomers(Table table)
{
    return table.Rows.Cast<TableRow>().Select(RowToCustomer);
} 
like image 141
agent-j Avatar answered Dec 08 '25 20:12

agent-j



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!