Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to apply projections on FK property in Entity Framework?
    text
    copied!<p>Using this <a href="https://stackoverflow.com/a/11978226/637677" title="answer">answer</a> I am now able to store my projection logic in an <code>Expression</code> and use it inside another projections.</p> <p>However, when I started to implement this approch in my solution, I found out, that I am not able to use the stored <code>Expression</code> on a Navigation property which is a single FK (not a collection).</p> <p>The following code demonstrates this issue:</p> <pre><code>namespace Entities { public class BlogPost { public virtual int BlogPostId { get; set; } public virtual string Title { get; set; } public virtual string NotUsed { get; set; } public virtual User Author { get; set; } } public class User { public virtual int UserId { get; set; } public virtual string Name { get; set; } public virtual string NotUsed { get; set; } public virtual ICollection&lt;BlogPost&gt; BlogPosts { get; set; } } } namespace Models { public class BlogPostModel { public string Title { get; set; } public UserModel Author { get; set; } } public class UserModel { public string Name { get; set; } } public static class BlogPostModelExtensions { public static readonly Expression&lt;Func&lt;BlogPost, BlogPostModel&gt;&gt; ToModelConverterExpression = p =&gt; new BlogPostModel { Title = p.Title, Author = null, //Problem! // I need to convert User (p.Author) to UserModel using UserModelExtensions.ToModelConverterExpression }; } public static class UserModelExtensions { public static readonly Expression&lt;Func&lt;User, UserModel&gt;&gt; ToModelConverterExpression = u =&gt; new UserModel{ Name = u.Name, }; } } </code></pre> <p>Is it possible to convert single FK navigation property to a model using <code>Expression</code>?</p>
 

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