2013-08-17 2 views
1

매개 변수없는 생성자를 반환하는 함수에서 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); 
} 

과 같이 호출하고 싶습니다

그래서 사실은 .. 그래서 아마 내가 모든 구성원 할당을하지 않고 다음과 같이 사용할 수있는 MemberInitExpression 매개 변수가없는 생성자로

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

를 변환하는 방법을

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); 
} 
+0

당신이 무엇을 요구 분명하지 않다. 무엇을 얻고 싶니? – xanatos

+0

매개 변수없는 생성자 정의 또는 함수에서 직접 'MemberInitExpression'을 얻는 방법을 찾고 있습니다. – sickdoctor

+0

MemberInitExpression은 다음과 같습니다. new Foo {Property1 = Something, Property2 = SomethingElse}, 아시겠습니까? – xanatos

답변

1
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 
    }; 
} 
+0

당신은 바로 선생님입니다. 나는 그것이 [this] (http://stackoverflow.com/questions/6736505/how-to-combine-two-lambdas/6736589#6736589)의 구현을 필요로한다고 생각합니다. LINQ에 묶여있다. – sickdoctor

관련 문제