1

I have a database request which looks as follows:

var userHasProfessions = xy.GetUserProfessions();

var users = sessionService.GetDefaultSession()
    .Query<User>()
    .Where(a => userProfessions.Contains(a.Profession.Id))
    .ToList();

It gets all users that the requesting user is allowed to see, depending on his own profession. Now I wanna restrict that a little more, that he can only see some attributes of the users. Let's say for example:

id        -> yes
firstname -> yes
lastname  -> yes
address   -> no!

Now I tried t change the query to something like:

var userHasProfessions = xy.GetUserProfessions();

var users = sessionService.GetDefaultSession()
    .QueryOver<User>()
    .SelectList(list => list
      .Select(a => a.id)
      .Select(a => a.firstname)
      .Select(a => a.lastname))
    .Where(a => userProfessions.Contains(a.Profession.Id))
    .ToList();

Now my question... Is there a way to, for example, make a new List with these attributes and then loop through it? Something like that:

List<string> attributes = new List<string>(){"id", "firstname", "lastname"}

var userHasProfessions = xy.GetUserProfessions();

var users = sessionService.GetDefaultSession()
    .QueryOver<User>()
    .SelectList(
      //loop through attributes
    )
    .Where(a => userProfessions.Contains(a.Profession.Id))
    .ToList();

Thanks in advance :-)

EDIT

To make my question a little bit more clear. I wanna have the attributes that the user is allowed to see, dynmically changeable from a List<string> outside the query.

How can a achieve that?

  • I don't know anything about NHibernate, but can't you do `sessionService.GetDefaultSession().QueryOver().Select(u => new { u.id, u.firstname, u.lastname})`? – DavidG Jun 19 '18 at 09:19
  • @DavidG Thanks!, your solution would work, but only if a hardcode the attributes in the query. I want to have them into a separate list. –  Jun 19 '18 at 09:59
  • I really don't understand what you're asking here. Are you looking to dynamically query the columns? – DavidG Jun 19 '18 at 10:02
  • @DavidG Exactly, I wanna have a List of the attibutes the user should see. This List is variable. That means the query should dynamically change the attributes allowed. –  Jun 19 '18 at 10:04
  • Then you're looking to use Dynamic Linq which I would strongly discourage. There's almost never a need to change the columns you require. – DavidG Jun 19 '18 at 10:06
  • @DavidG Can you give me a useful example to do this with dynamic LINQ? Maybe as an answer so I could approve it. –  Jun 19 '18 at 14:06
  • Not really no. The problem is that even anonymous types are creates at compile time. There's some hacky solutions [here](https://stackoverflow.com/questions/9000753/how-can-i-create-a-dynamic-multi-property-select-on-an-ienumerablet-at-runtime) but like I said above, are you really sure you need this? – DavidG Jun 19 '18 at 14:13
  • @DavidG Actually I need it, because I have to display different attributes to users in different roles... –  Jun 19 '18 at 14:17

1 Answers1

0

You could do

.Where(a => userProfessions.Contains(a.Profession.Id))
.Select(c => new User {
    id = c.id,
    firstname = c.firstname,
    lastname = c.lastname,
    address   c.address
}).ToList();

I am assuming list is type of User. Other wise you could use anonymous return i.d. c => new { id="id",... }

Manoz
  • 6,507
  • 13
  • 68
  • 114
  • No List is type of string. My intention is to have the List externally in an variable, so I don't have to hardcode them in the query. Any other idea? –  Jun 19 '18 at 10:01
  • @RaphaelMüller, what is your expected output – Manoz Jun 19 '18 at 10:46
  • I expect a user model that would look in JSON like `[{"id":1,"firstname":"Paul","lastname":"Paulsen","address":null}]` or without the address, that's not that important. –  Jun 19 '18 at 10:53
  • @RaphaelMüller, `.Select(u => new { u.id, u.firstname, u.lastname})` would work in that case – Manoz Jun 19 '18 at 12:13