简体   繁体   English

无法在C#中调用扩展方法

[英]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 ? 我该如何解决?

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. 您在IEnumerable接口中注册了扩展方法(由方法定义中的以下参数指示: this IEnumerable<T> source ),同时(根据错误消息判断)尝试从DataTable实例调用它。 The error is appearing simply because DataTable does not implement IEnumerable ( source ). 出现该错误仅仅是因为DataTable没有实现IEnumerablesource )。 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. 只需将IEnumerable替换为DataTable或由DataTable类实现的其他接口,使您的方法适应这些更改,该方法就可以正常工作。

first convert dtTA to IEnumerable source then , you can call ToPivotTable by source like following(holp this helpful) : 首先将dtTA转换为IEnumerable源,然后可以通过以下源调用ToPivotTable(此功能很有帮助):

//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);

Would it not be the case that you transform the DataTable into an IEnumerable via the AsEnumerable method? 您不是通过AsEnumerable方法将DataTable转换为IEnumerable的情况吗?

Am I missing something? 我想念什么吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM