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";
}
Never mind. I figured it out. Here it is for anyone it might help in the future: