How to resolve ambigiously named extension method?
Asked Answered
D

2

5

I have a DataTable that I'm trying to enumerate over with the AsEnumerable extension method on System.Linq.Enumerable. The problem is that there is an identically named extension method on System.Data.DataTableExtensions. I need to use both namespaces in my class so removing one of the using statements is not an option.

How do I declare that I want the AsEnumerable method from System.Linq.Enumerable and not the System.Data.DataTableExtensions?

Deictic answered 4/6, 2010 at 19:24 Comment(0)
H
3

They're just static methods so you could do this:

DataTable dt;
System.Linq.Enumerable.AsEnumerable(dt);
Hawkweed answered 4/6, 2010 at 19:28 Comment(1)
Thanks, I'll accept that as the answer. I was hoping there was a way to still use the extension method though.Deictic
M
4

DataTable does not implement IEnumerable<T>, or even IEnumerable, so you cannot call Enumerable.AsEnumerable() on it directly. That is what DataTableExtensions.AsEnumerable() is for in the first place.

Meretricious answered 4/6, 2010 at 19:41 Comment(1)
I think you are right. For some reason VS was giving me the ambiguous error for that method which prompted the question, and now I can't recreate it. I accidentally voted you down, if you edit your answer I'll vote you back up.Deictic
H
3

They're just static methods so you could do this:

DataTable dt;
System.Linq.Enumerable.AsEnumerable(dt);
Hawkweed answered 4/6, 2010 at 19:28 Comment(1)
Thanks, I'll accept that as the answer. I was hoping there was a way to still use the extension method though.Deictic

© 2022 - 2024 — McMap. All rights reserved.