Create a method that takes an entity model, a property of an entity mode, and a DataSourceResult as parameter to determine a column width

18 views Asked by At

I am using Telerik UI for Blazor grid and trying to create a method that will take the data brought back from a linq query, look at all the strings in a specific column to find the largest one, multiply it by a set value, check to see if it is larger than a minimum value I have to set to ensure the column title is not cut off and unreadable, and finally set the value to a string plus the text "px" to be used as a column width setting. I want to pass a generic entity instead of having to specify a specific entity. How do I do so?

@using System.Linq.Expressions
@using LinqKit

public string GetColumnWidth(int MinTitleWidth, Expression<Func<MyEntityModel, string>> selector, DataSourceResult dataSourceResult)
{
    int MinColumnTitleWidthCheck = dataSourceResult.Data.Cast<MyEntityModel>()
        .Select(i => selector.Invoke(i))
        .ToList()
        .Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur)
        .Length * 12;
    MinColumnTitleWidthCheck = MinColumnTitleWidthCheck > MinTitleWidth ? MinColumnTitleWidthCheck : MinTitleWidth;
    return MinColumnTitleWidthCheck.ToString() + "px";
}
1

There are 1 answers

0
BlueCardinal On

Never mind. I figured it out. Here it is for anyone it might help in the future:

@using System.Linq.Expressions
@using LinqKit

public string GetColumnWidth<T>(int MinTitleWidth, Expression<Func<T, string>> selector, DataSourceResult dataSourceResult)
{
    int MinColumnTitleWidthCheck = dataSourceResult.Data.Cast<T>()
        .Select(i => selector.Invoke(i))
        .ToList()
        .Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur)
        .Length * 12;
    MinColumnTitleWidthCheck = MinColumnTitleWidthCheck > MinTitleWidth ? MinColumnTitleWidthCheck : MinTitleWidth;
    return MinColumnTitleWidthCheck.ToString() + "px";
}