0

I have the following query:

var list = (
        from au in context.AnagraficaUtenti
        join ut in context.Utenti on au.CodiceUtente equals ut.CodiceUtente into allcolumns
        from entry in allcolumns
        select entry
    )
    .ToList();

the problem is that both AnagraficaUtenti and Utenti have two columns called "Name" and "Surname", so the list I get contains only one of them. I would like to obtain a list of objects whose fields will be "NameAnagraficaUtenti", "SurnameAnagraficUtenti", "NameUtenti" and "SurnameUtenti" (among all the others).

Is that possible? Thank you.

P.S. I know that I can select fields one by one, by doing something like "new {field1 = field1, field2 = field2}", but I'd love if I could accomplish the same result with a general "select all", thank you.

Alessandro
  • 107
  • 1
  • 8
  • 1
    You can select whatever objects you wish, so i.e. select a new object with these new names. see https://stackoverflow.com/questions/6370028/return-list-using-select-new-in-linq – MountainGoat Nov 17 '22 at 09:20
  • yes, you are right, but I'd like not to write fields one by one. – Alessandro Nov 17 '22 at 09:21
  • 1
    "Is that possible?" - **no**, it is not. But why is that a problem? And why should names matter at this point? – Dai Nov 17 '22 at 09:22
  • @Dai 'cause clients change requirements all the time, and it'd be a pain to change SQL code each time they do that – Alessandro Nov 17 '22 at 09:27
  • 1
    @Alessandro Your contracting clients should not be concerned with matters of internal representation - if they do then you stop being a contractor and start being an employee, which means (under US law, at least) they owe you benefits etc - so feel free to bring that up next time they annoy you :) – Dai Nov 17 '22 at 09:28
  • `GroupJoin` is not needed here. Just do normal join. – Svyatoslav Danyliv Nov 17 '22 at 10:17
  • 1
    I don't see where you are returning any columns from `AnagraficaUtenti`: you have `select entry` and `entry` is from `allcolumns` and `allcolumns` is from `join ut in context.Utenti`...`into allcolumns` which means you are just returning columns from `Utenti`. If you want columns from `AnagraficaUtenti`, you must specify them like `select new { au, entry }` then you can have `list[0].au.Name` and `list[0].entry.Name`. You could use `select new { AnagraficaUtenti = au, Utenti = entry }` and use `list[0].AnagraficaUtenti.Name` and `list[0].Utenti.Name` instead. – NetMage Nov 17 '22 at 21:07
  • 1
    PS It is possible to do what you want, but it is a big step into Reflection and `Expression` tree handling. – NetMage Nov 17 '22 at 21:08

0 Answers0