user8189013
user8189013

Reputation:

Can not call Extension Method in c#

I have following class;

public static class statiClass
{
    public static DataTable ToPivotTable<T, TColumn, TRow, TData>(
 this IEnumerable<T> source,
 Func<T, TColumn> columnSelector,
 Expression<Func<T, TRow>> rowSelector,
 Func<IEnumerable<T>, TData> dataSelector)
    {
    DataTable table = new DataTable();
    var rowName = ((MemberExpression)rowSelector.Body).Member.Name;
    table.Columns.Add(new DataColumn(rowName));
    var columns = source.Select(columnSelector).Distinct();

    foreach (var column in columns)
        table.Columns.Add(new DataColumn(column.ToString()));

    var rows = source.GroupBy(rowSelector.Compile())
                     .Select(rowGroup => new
                     {
                         Key = rowGroup.Key,
                         Values = columns.GroupJoin(
                             rowGroup,
                             c => c,
                             r => columnSelector(r),
                             (c, columnGroup) => dataSelector(columnGroup))
                     });

    foreach (var row in rows)
    {
        var dataRow = table.NewRow();
        var items = row.Values.Cast<object>().ToList();
        items.Insert(0, row.Key);
        dataRow.ItemArray = items.ToArray();
        table.Rows.Add(dataRow);
    }

    return table;
}

And I can not call him from another class like below;

dtTA.ToPivotTable(
                item => item.Year,
               item => item.Product,
               items => items.Any() ? items.Sum(x => x.Sales) : 0);

It throws an error like ;

DataTable' does not contain a definition for 'ToPivotTable' and no accessible extension method 'ToPivotTable'

I searched a lot and didn't find any solution interestingly.

How can i solve this ?

Upvotes: 0

Views: 221

Answers (3)

MacroMarc
MacroMarc

Reputation: 3324

Would it not be the case that you transform the DataTable into an IEnumerable via the AsEnumerable method?

Am I missing something?

dtTA.AsEnumerable().ToPivotTable(
           item => item.Year,
           item => item.Product,
           items => items.Any() ? items.Sum(x => x.Sales) : 0);

Upvotes: 0

windfog
windfog

Reputation: 209

first convert dtTA to IEnumerable source then , you can call ToPivotTable by source like following(holp this helpful) :

//first convert dtTA to IEnumerable source
        var source = dtTA
            .Select()
            .Select(x => new 
            {
                Year = (string)x["Year"],
                Product = (string)x["Product"],
                Sales = (int)x["Sales"]
            }).ToList();
        //then , you can call ToPivotTable by source
        source.ToPivotTable(
            item => item.Year,
           item => item.Product,
           items => items.Any() ? items.Sum(x => x.Sales) : 0);

Upvotes: 2

Szab
Szab

Reputation: 1263

You register your extension method in IEnumerable interface (indicated by the following argument in the method definition: this IEnumerable<T> source), while (judging by the error message) trying to call it from a DataTable instance. The error is appearing simply because DataTable does not implement IEnumerable (source). Simply replace IEnumerable with DataTable or other interface which is implemented by the DataTable class, adapt your method to these changes and the method should work just fine.

Upvotes: 2

Related Questions