Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I call a method from within a LINQ expression?
    primarykey
    data
    text
    <p>I've my Model something maybe like this :</p> <pre><code> public class Person { public virtual Guid ID { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } //this LazyList takes IQueryable&lt;T&gt; in constructor public virtual LazyList&lt;Role&gt; Roles { get; set; } //this method to convert the object that comes from EF to my own model public static Person FromData(DBPerson dbPerson) { if (dbPerson != null) return new Person { ID = dbPerson.ID, FirstName = dbPerson.FirstName, LastName = dbPerson.LastName, }; return null; } } </code></pre> <p>Then in my Repository I've method to get all persons but i need it to give to the <code>LazyList&lt;Role&gt;</code> property result from other private method "getRoles". My repository looks like this:</p> <pre><code> public IQueryable&lt;Person&gt; GetPersons() { var list = from dbPerson in context.Persons select Person.FromData(dbPerson); // Here is the problem :( return list; } private LazyList&lt;Role&gt; getRoles(GUID userID) { var list = from role in db.Roles where role.UserID == userID select role.ToModel(); return new LazyList&lt;Role&gt;(list); } </code></pre> <p><strong>I want something here to tell this Linq statement that i want Person.Roles to get its value from "getRoles" method</strong></p> <p>Maybe someone will tell me to put the logic of converting the dbPerson to my Person in the repository and then put something like this :</p> <pre><code> public IQueryable&lt;Person&gt; GetPersons() { var list = from dbPerson in context.Persons select new Person{ ID = dbPerson.ID, FirstName = dbPerson.FirstName, LastName = dbPerson.LastName, Roles = getRoles(dbPerson.ID)}; } </code></pre> <p>OK that will work but i really need to put the logic of converting dbPerson to my model in separate method because i'll sue it many times in my solution.</p> <p>So how to do that ?</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.
 

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