Simon
Simon

Reputation: 1476

calling a function from within a lambda

Is there a way to call a function from within a lambda expression?

we currently have the following

 var obj = m.Select(x => x.ToDictionary(y => y.ColumnName, y => y.ColumnValue))
             .ToList();


private static string BuildNewContent(EnumColumnType columnType, String columnValue, List<TableColumn> CurrentRow)
    {
}

so what we really want to do is add the returned value from BuildNewContent as the value for the dictionary. so something like the following:-

var obj = m.Select(x => x.ToDictionary(y => y.ColumnName, BuildNewContent(y => y.ColumnType, y=> y.ContentValue, y)))
             .ToList();

I realize that we can easily achieve this via simply looping, however, we would like to see how to do this using Lambda (if possible)

UPDATE - As requested the type of M is a TableViewModel which is a nested list

public class TableViewModel 
{
   public List<List<TableColumn>> Grid { get; set; }
}

 public class TableColumn
{
    public TableColumn() { }

    public TableColumn(string columnHeader, string columnValue, int columnWidth, EnumColumnType columnType, string columnName)
    {
        this.ColumnHeader = columnHeader;
        this.ColumnValue = columnValue;
        this.ColumnWidth = columnWidth;
        this.ColumnType = columnType;
        this.ColumnName = columnName;
    }

    public string               ColumnHeader    { get; set; }
    public string               ColumnName      { get; set; }
    public string               ColumnValue     { get; set; }
    public int                  ColumnWidth     { get; set; }
    public EnumColumnType       ColumnType      { get; set; }  
}

Upvotes: 1

Views: 146

Answers (2)

peter
peter

Reputation: 15079

you had it almost right

var obj = m.Select(x => x.ToDictionary(
    y => y.ColumnName, y => BuildNewContent(y.ColumnType, y.ContentValue, y))).ToList();

however I suspect that instead of the last y parameter you want to provide x or some other value, since y probably isn't of type List<TableColumn>

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1499790

The problem with what you've tried is just the arguments to the BuildNewContent method - you're providing lambda expressions when they should just be values. So I suspect you want:

var obj = m.Select(x => x.ToDictionary(y => y.ColumnName,
                               BuildNewContent(y.ColumnType, y.ContentValue, y)))
           .ToList();

It's not clear what the type of y is, so it's hard to know whether this will really just work immediately, but it's certainly entirely feasible to call a method within a lambda expression.

However, this will fail at execution time if m is something like a LINQ to SQL table - in that case, the LINQ provide will need to try to convert the expression tree (that the compiler has created from the lambda expression) into SQL, and it doesn't know what BuildNewContent is meant to do. If it's just LINQ to Objects, that's fine.

Upvotes: 2

Related Questions