Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Updated with statements that can be executed or deferred.

// Example models
public class SomeModel
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List Items { get; set; }
}

public class SomeOtherModel
{
    public int Id { get; set; }
    public string Text { get; set; }
}

//Expression mapper - not executed
public Expr<Func> Map_SomeTable_SomeModel_Expr()
{
    return x => new SomeModel
    {
        Id = x.Id,
        Name = x.Name,
        Items = db.SomeOtherTable.Where(y => y.SomeTableId == x.Id).Select(Map_SomeOtherTable_SomeOtherModel_Expr()).ToList()
    }
}

//Function mapper - compiled and makes the query execute; 
//    can also be obtained by calling Compile() on an expression;
//    can be called as a function
public Func Map_SomeTable_SomeModel_Func = x => new SomeModel
{
    Id = x.Id,
    Name = x.Name,
    Items = db.SomeOtherTable.Where(y => y.SomeTableId == x.Id).Select(Map_SomeOtherTable_SomeOtherModel_Func()).ToList()
};

public Expr<Func> Map_SomeOtherTable_SomeOtherModel_Expr()
{
    return x => new SomeOtherModel
    {
        Id = x.Id,
        Text = x.Text
    }
}


public Func Map_SomeOtherTable_SomeOtherModel_Func = x => new SomeOtherModel
{
    Id = x.Id,
    Text = x.Text
};

//Example repository method
public List GetAllSomeModels()
{
    var query = db.SomeTable.Select(Map_SomeTable_SomeModel_Expr()); // Is saved as query and not executed
    var model = db.SomeTable.Select(Map_SomeTable_SomeModel_Func); // Query is executed and result set is returned
}