Execute .Select () in IQueryable with the delegate list Func <T, string>

First of all, let me apologize if the title does not make sense. It's hard for me to understand what I'm doing, not to mention using the right words to describe what I'm doing.

I create a generic grid class in which I define columns using a combination of header / linq expressions:

public class Column<T>
{
    public string Header { get; set; }
    public Func<T, string> ValueExpression { get; set; }
}

Using:

Columns = new List<Column<Employee>>
              {
                  new Column<Employee> {Header = "Employee Id", ValueExpression = e => e.EmployeeID.ToString()},
                  new Column<Employee> {Header = "Name", ValueExpression = e => e.FirstName + " " + e.LastName},
                  new Column<Employee> {Header = "Employee Birthday Year", ValueExpression = e => e.BirthDate.HasValue ? e.BirthDate.Value.Year.ToString() : ""}
              },

I want to design ValueExpression ( Func<T, string>) for IQueryable employees:

var db = new NorthwindDataContext();
var employees = db.Employees.Select(e => e);

I can get this to work by extracting IEnumerable<IEnumerable<string>>from employees (a list of lists of strings used in my views), for example:

var elementsList = employees.ToPagedList(PageIndex, PageSize);
var elementStringList = elementsList.ToStringList(Columns.Select(c => c.ValueExpression).ToArray());

Do not pay attention to the PagedList material, it does not matter and does not look like ToList ();

Here's the ToStringList () Extension method:

public static IEnumerable<IEnumerable<string>> ToStringList<T>(this IPagedList<T> enumerable, params Func<T, string>[] fields)
{
    foreach (var element in enumerable)
        yield return element.ToStringList(fields);
}

private static IEnumerable<string> ToStringList<T>(this T element, params Func<T, string>[] fields)
{
    foreach (var field in fields)
        yield return field(element);
}

, IQueryable , .

- :

SELECT [t0].[EmployeeID], [t0].[LastName], [t0].[FirstName], [t0].[Title], [t0].[TitleOfCourtesy], [t0].[BirthDate], [t0].[HireDate], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[HomePhone], [t0].[Extension], [t0].[Photo], [t0].[Notes], [t0].[ReportsTo], [t0].[PhotoPath]
FROM [dbo].[Employees] AS [t0]

, Employees.

- IQueryable, List of Func ( ValueExpression ) "" IQuerable , SQL, .

, - :

IQueryable employees = employees.SelectByExpressions(Columns.Select(c => c.ValueExpression).ToArray());

.

+3
4

, , , Select , , . , , . , , .

, , , , , . O/R, LINQ-to-SQL, DataSets, , , , , . , , , .

, . , , . , , , , , . , . , , , .

: , - . , - " ", , /, DataRow, , , , . , . , , .

+1
0

. , :

LINQ

.

0

Linq. , , .

, , select, sql.

0

Source: https://habr.com/ru/post/1708695/


All Articles