如何将无参数构造函数转换为MemberInitExpression

医生

我可以MemberInitExpression从实际上返回无参数构造函数的函数中创建无参数构造函数或任何表达式吗?

public IQueryable<GroupView> GetViewSelect(IQueryable<ItemGroup> myQ)
{
    return myQ.SelectMany(GetExpression1(), GetExpression2());
}        

internal static Expression<Func<ItemGroup, IEnumerable<ItemDetail>>> 
    GetExpression1()
{
    // m is the ItemGroup
    // m.item is Item
    // m.item.itemDetail is a collection of ItemDetail
    return m => m.item.itemDetail; 
}

internal static Expression<Func<ItemGroup, ItemDetail, GroupView>> 
    GetExpression2()
{
    // m is the ItemGroup
    // n is the ItemDetail
    // and it's error at runtime coz LINQ doesnt know the GetGroupView.. :(
    return (m, n) => GetGroupView(m, n);
}

internal static GroupView GetGroupView(ItemGroup m, ItemDetail n)
{
    // I think the same error will occurs
    // coz LINQ doesnt know the GetItemView and GetItemDetailView
    return new GroupView
    {
        Id = m.id,
        Name = m.name,
        ItemDetailsTotalCount = 
            m.item.ItemDetails.Sum(nn => nn.item.itemDetails.Count),

        Item = GetItemView(m.item),
        ItemDetail = GetItemDetailView(n)
    };
}    

internal static ItemView GetItemView(Item m)
{
    return new ItemView
    {
        Id = m.id,
        Name = m.name,
        DetailCount = m.ItemDetail.Count
    };
}

internal static ItemDetailView GetItemDetailView(ItemDetail n)
{
    return new ItemDetailView
    {
        Id = n.id,
        Name = n.name,            
        Supplier = GetSupplierView(n.supplier)
    };
}

internal static SupplierView GetSupplierView(Supplier n)
{
    return new SupplierView
    {
        Id = n.id,
        Name = n.name,
        Email = n.email ?? "no email",
        Phone = n.phone ?? "no phone"
    };
} 

当然,上述所有方法都不起作用,但是我只是想避免一遍又一遍地重新键入相同的无参数构造函数,以便每次我想要获得不同的视图时都获得视图构造函数。

例如,我想这样称呼它

public IQueryable<ItemView> GetViewSelect(IQueryable<Item> myQ)
{
    return myQ.Select(GetExpression3());
}        

public IQueryable<ItemView> GetViewSelect(IQueryable<ItemDetail> myQ)
{
    return myQ.Select(GetExpression3());
}        

internal static Expression<Func<Item, ItemView>> GetExpression3()
{
    return m => GetItemView(m); 
}

internal static Expression<Func<ItemDetail, ItemView>> GetExpression4()
{
    return m => GetItemView(m.item);
}

而不是每次我像下面这样调用它时都编写相同的无参数构造函数。

internal static Expression<Func<Item, ItemView>> GetExpression3()
{
    // writing same parameterless constructor again
    return m => new ItemView
    {
        Id = m.id,
        Name = m.name,
        DetailCount = m.ItemDetail.Count
    }; 
}

internal static Expression<Func<ItemDetail, ItemView>> GetExpression4()
{
    // the same thing again
    return m => new ItemView
    {
        Id = m.item.id,
        Name = m.item.name,
        DetailCount = m.item.ItemDetail.Count
    };
}

internal static Expression<Func<ItemGroup, ItemDetail, GroupView>> 
    GetExpression2()
{
    return (m, n) => new GroupView
    {
        Id = m.id,
        Name = m.name,
        ItemDetailsTotalCount = 
            m.item.ItemDetails.Sum(nn => nn.item.itemDetails.Count),

        Item = new ItemView
        {
            Id = m.item.id,
            Name = m.item.name,
            DetailCount = m.item.ItemDetail.Count
        },
        ItemDetail = new ItemDetailView
        {
            Id = n.id,
            Name = n.name,            
            Supplier = new SupplierView
            {
                Id = n.id,
                Name = n.name,
                Email = n.email ?? "no email",
                Phone = n.phone ?? "no phone"
            }
        }
    };
}

所以我实际上是在寻找一种转换的方法

new ItemView
{
    Id = m.id,
    Name = m.name,
    DetailCount = m.ItemDetail.Count
}

到无MemberInitExpression参数的构造函数中,所以也许我可以像下面那样使用它,而无需进行任何成员分配。

internal Expression<Func<Item, ItemView>> GetExpression4e
    (ParameterExpression m)
{
    // looking for something like this.. 
    MemberInitExpression ItemViewInitExpression = 
        Something("GetItemView", new Object[] {m});

    return Expression.Lambda<Func<Item, ItemView>>
        (ItemViewInitExpression, m);
}
Xanatos
internal static Expression<Func<Item, ItemView>> GetExpression3()
{
    // writing same parameterless constructor again
    return m => new ItemView
    {
        Id = m.id,
        Name = m.name,
        DetailCount = m.ItemDetail.Count
    }; 
}

此方法已经产生了MemberInitExpression如果你这样做

var type = GetExpression3().Body.GetType();

你会得到 System.Linq.Expressions.MemberInitExpression

如果只需要传递一些参数,则传递一些参数:

internal static Expression<Func<Item, ItemView>> GetExpression3(int id, string name, int count)    
{
    // writing same parameterless constructor again
    return m => new ItemView
    {
        Id = id,
        Name = name,
        DetailCount = count
    }; 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将值列表转换为数据构造函数的参数?

如何将JavaScript构造函数转换为Amber Smalltalk?

如何将值从构造函数转换为方法?

如何将函数参数转换为键/值对?

如何将无符号long *参数转换为ulong []参数

如何将无参数构造函数的对象放置到std :: map中?

如何将转换转换为函数?

如何将包含构造函数的C ++类转换为C结构?

如何将向量转换为函数

如何将模板参数包转换为函数的多个指针参数?

如何将IEnumerable(无类型)转换为IQueryable <T>?

如何将Int转换为无符号字节并返回

如何将Python的“无”类型转换为“空”类型

如何将整数类型转换为无符号类型?

如何将字段构造函数参数传递给函数?

如何将可变参数函数参数转换为数组?

如何将参数表达式转换为函数?

如何将字符串转换为参数名称并将其传递给函数?

如何将模板类参数转换为const参数

如何将列表转换为可变参数参数java

如何将具有派生参数的函数转换为具有基本参数的函数?

如何解决“无参数构造函数”

如何将参数传递给canActivate的构造函数?

这个构造函数代码如何将指针作为参数?

如何将构造函数参数注入集合夹具中?

如何将Class构造函数或Type作为参数传递

如何将参数传递给静态类构造函数?

如何将构造函数参数添加到列表?

如何将构造函数参数绑定到实例字段?