question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked TZacks-2728 commented

How to convert a code which will work in .net v4.5.2

I have this code got from a site which is not compiling in .net v4.5.2. please have a look.

 public static DataTable ToPivotTable<T, TPivot>(
     this IEnumerable<T> source,
     IReadOnlyList<Expression<Func<T, object>>> groupColumns,
     Func<T, string> pivotColumn, Func<T, TPivot> pivotValue)
 {
     var result = new DataTable();
     var groupColumnMembers = groupColumns.Select(c => ((MemberExpression)c.Body).Member).Cast<PropertyInfo>().ToList();
     foreach (var groupColumn in groupColumnMembers)
     {
         result.Columns.Add(new DataColumn(groupColumn.Name, groupColumn.PropertyType));
     }
        
     var p = Expression.Parameter(typeof(T), "p");
     var tupleType = Type.GetType($"System.Tuple`{groupColumns.Count}");
     var groupColumnTypes = groupColumnMembers.Select(x => x.PropertyType).ToArray();
     var tupleConstructor = tupleType.MakeGenericType(groupColumnTypes).GetConstructor(groupColumnTypes);
            
     var args = groupColumnMembers.Select(c => Expression.Property(p, c));
     var body = Expression.New(tupleConstructor, args);
     var groupByExpression = Expression.Lambda<Func<T, System.Runtime.CompilerServices.ITuple>>(body, p);
        
     foreach (var group in source.GroupBy(groupByExpression.Compile()))
     {
         foreach (T element in group)
         {
             string pivotColumnName = pivotColumn(element);
             if (!result.Columns.Contains(pivotColumnName))
             {
                 result.Columns.Add(new DataColumn(pivotColumnName, typeof(TPivot)));
             }
         }
            
         var row = result.NewRow();
         for (int index = 0; index < group.Key.Length; index++)
         {
             row[index] = group.Key[index];
         }
            
         foreach (T element in group)
         {
             string pivotColumnName = pivotColumn(element);
             TPivot pivotColumnValue = pivotValue(element);
             row[pivotColumnName] = pivotColumnValue;
         }
            
         result.Rows.Add(row);
     }
        
     return result;
 }

Calling this way

 DataTable pivotedData = _data.ToPivotTable(
     new List<Expression<Func<Data, object>>>
     {
         d => d.Section, 
         d => d.Lineitem, 
         d => d.BrokerCode, 
         d => d.BrokerName 
     }, 
     d => d.Period, 
     d => d.PeriodValue);

When i try to compile above code then i am getting error for these below line which may work in .net upper version.

 var tupleType = Type.GetType($"System.Tuple`{groupColumns.Count}");
  var groupByExpression = Expression.Lambda<Func<T, System.Runtime.CompilerServices.ITuple>>(body, p);
     
  foreach (var group in source.GroupBy(groupByExpression.Compile()))
  {
  }

Please some one help me to convert the above code as a result it should work in .net version 4.5.2

Thanks




dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered TZacks-2728 commented

@TZacks-2728, according to the Microsoft doc, ITuple Interface 's lowest .net framework version is .net framework 4.7.1.

Currently, it is the best way for you to solve the problem to upgrade your .net framework verison to 4.7.1 or later.

Best Regards,
Jack


If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JackJJun-MSFT see this code https://techbrij.com/pivot-c-array-datatable-convert-column-to-row-linq
from the above link sir please see this function body ToPivotTable()
calling this way var pivotTable = data.ToPivotTable(
item => item.Year,
item => item.Product,
items => items.Any() ? items.Sum(x=>x.Sales) : 0);


Main problem of the above ToPivotTable() function that it does not accept multiple fields name on which grouping will be performed.

can you please customize the code of ToPivotTable() function as a result it should accept multiple fields name on which grouping will be performed and should work also in .net 4.5.2 version.

waiting for your reply. thanks


0 Votes 0 ·

is it really tough for any experience developer to translate my posted code to similar one which will work in .net v4.5.2?

0 Votes 0 ·

@TZacks-2728 , I am doing some researching about your problem. By the way, I have a question. What is your desired datatable included many columns? Do you want a sum datatable? Or is it your desired datatable?

133468-image.png


0 Votes 0 ·
image.png (29.9 KiB)

Sir i am looking for a common functionality in a function like ToPivotTable<T, TPivot>() which would arrange my data in pivot format. i am looking for a extension method which would take datatable or List<T> and return data in pivot format.

ToPivotTable<T, TPivot>() is good one but it is not compiling in ,net v4.5.2.

i believe if some one tweak the code then it can compile. can you please share a function like ToPivotTable<T, TPivot>() which would work in
net v4.5.2. and can take multiple field name for grouping and return data in pivot format always. thanks



0 Votes 0 ·