Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to SQL construct a custom object externally - join from another object
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/6760986/linq-to-sql-construct-a-custom-object-externally">Continued from this solution (thanks Daniel Hilgarth)</a></p> <pre><code>return db.Tags.Select(ConstructTagItem()); </code></pre> <p>And the method:</p> <pre><code>private Expression&lt;Func&lt;Tag, TagItem&gt;&gt; ConstructTagItem() { return a =&gt; new TagItem {ID = a.Id Name = a.Name }; } </code></pre> <hr> <p><strong>Additional question, how do i use it in this scenario then:</strong> </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 = new TagItem { ID = c.Tag.Id, Name = c.Tag.Name } }); </code></pre> <p>I want to reuse the method from the other answer:</p> <pre><code>private Expression&lt;Func&lt;Tag, TagItem&gt;&gt; ConstructTagItem { get { return a =&gt; new TagItem {ID = a.Id Name = a.Name }; } } </code></pre> <p>To construct something like this:</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 = ConstructTagItem // TODO: need some way to tell the method that it should use c.Tag }); </code></pre> <p>I want to use the same construction of my TagItem multiple places. This will make it easier if the object changes, and save lines.</p> <p>I guess that I somehow have to define that it is <code>c.Tag</code> into <code>ConstructTagItem()</code>, but I really don't know much about expressions yet. So i hope that someone is able to help?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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