I’m trying to create a CSV extension method for my enumerable list and I’m stumped. Here’s how I created my simple enumerated list:
var CAquery = from temp in CAtemp
join casect in CAdb.sectors
on temp.sector_code equals casect.sector_code
select new
{
CUSIP = temp.equity_cusip,
CompName = temp.company_name,
Exchange = temp.primary_exchange
};
CAquery.WriteToCSVFile();
This is what I have done so far in creating an extension method (which I think is wrong):
public static class CSVExtensions
{
public static void WriteToCSVFile(this IEnumerable<T> myList)
{
Do you see what I’m doing wrong?
You have to specify the generic type parameter in the method signature:
Are you truly trying to write an extension method that should work on any
IEnumerable<T>or is your type more specific? If the later is the case you should replaceTwith the type you want to support (or add sufficient constraints).Edit:
In light of comments – you should project to a class instead of an anonymous type in your query – then you can use an extension method for this particular type, i.e.:
Now your query can be:
And your extension method (which now doesn’t need to be generic) becomes: