2

Continued from this solution (thanks Daniel Hilgarth)

return db.Tags.Select(ConstructTagItem());

And the method:

private Expression<Func<Tag, TagItem>> ConstructTagItem()
{
    return a => new TagItem {ID = a.Id Name = a.Name };
}

Additional question, how do i use it in this scenario then:

return (from c in db.News_Attributes
        select new NewsTagItem
        {
            NewsID = c.News_Id,
            TagID = c.Tag_Id,
            Content = c.Content,
            Attribute = new TagItem
            {
                ID = c.Tag.Id,
                Name = c.Tag.Name
            }
        });

I want to reuse the method from the other answer:

private Expression<Func<Tag, TagItem>> ConstructTagItem
{
    get { return a => new TagItem {ID = a.Id Name = a.Name }; }
}

To construct something like this:

return (from c in db.News_Attributes
        select new NewsTagItem
        {
            NewsID = c.News_Id,
            TagID = c.Tag_Id,
            Content = c.Content,
            Attribute = ConstructTagItem // TODO: need some way to tell the method that it should use c.Tag
        });

I want to use the same construction of my TagItem multiple places. This will make it easier if the object changes, and save lines.

I guess that I somehow have to define that it is c.Tag into ConstructTagItem(), but I really don't know much about expressions yet. So i hope that someone is able to help?

1 Answer 1

0

I'm not sure if I have a full handle on what you're trying to do. What does "use it in this scenario" mean? Can you mimic your previous technique with something like this in order to encapsulate creating a NewsTagItem, or is it something else you're trying to achieve?

private Expression<Func<News_Attribute, NewsTagItem>> ConstructNewsTagItem()
{
    return c => new NewsTagItem 
    {
        NewsID = c.News_Id,
        Name = a.Name 
        TagID = c.Tag_Id,
        Content = c.Content,
        Attribute = new TagItem
        {
            ID = c.Tag.Id,
            Name = c.Tag.Name
        }
    }
});

db.News_Attributes.Select(ConstructNewsTagItem());

UPDATE:

OK, we can't directly re-use your ConstructTagItem() because it returns an expression containing a function. What you need is a MemberInitExpression. It's a little tricky to create by hand, but we can use a trick whereby we create the expression we desire wrapped with a thunk, so that it isn't evaluated, and then grab the body of the thunk to get the expression. See the snippet below:

private Expression GenerateNewTagItem(TagItem c)
{
    Expression<Func<TagItem>> expr = () => new TagItem { ID = c.ID, Name = c.Name };
    return expr.Body;
}

With this function, we can now do pretty much exactly what you want:

return (from c in db.News_Attributes
    select new NewsTagItem
    {
        NewsID = c.News_Id,
        TagID = c.Tag_Id,
        Content = c.Content,
        Attribute = GenerateNewTagItem(c)
    });

Pretty neat right?

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, after much reading and re-reading your question, I think I understand what you're trying to get at. 1 Answer coming up...
It looks right! but I get one problem, it can't convert the expression to the property Attribute, which in this case it TagItem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.