Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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?</p> <pre><code>private Expression&lt;Func&lt;News_Attribute, NewsTagItem&gt;&gt; ConstructNewsTagItem() { return c =&gt; 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()); </code></pre> <p><strong>UPDATE:</strong> </p> <p>OK, we can't directly re-use your ConstructTagItem() because it returns an expression containing a function. What you need is a <code>MemberInitExpression</code>. 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:</p> <pre><code>private Expression GenerateNewTagItem(TagItem c) { Expression&lt;Func&lt;TagItem&gt;&gt; expr = () =&gt; new TagItem { ID = c.ID, Name = c.Name }; return expr.Body; } </code></pre> <p>With this function, we can now do pretty much exactly what you want:</p> <pre><code>return (from c in db.News_Attributes select new NewsTagItem { NewsID = c.News_Id, TagID = c.Tag_Id, Content = c.Content, Attribute = GenerateNewTagItem(c) }); </code></pre> <p>Pretty neat right?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload