1

I am using this code in order to query through the database and inserting the values into a model:

  UserContext user = new UserContext();

 List<MitarbeiterDataModel> mitarbeiter = new List<MitarbeiterDataModel>();
 var users = user.Users.Select(x => new MitarbeiterDataModel{ Id = x.Id, Vorname = x.Vorname, Nachname = x.Nachname, Studio = x.Studio }).ToList();
 mitarbeiter.AddRange(users);

Now I have a list of employees which looks like this for example:

------------------------------
| Id | Name | Opportunities  |
------------------------------
| 1  | Tom  |                |
------------------------------
| 2  |John  |                |
------------------------------

My goal is to fill the Opportunities column as well. The problem is that these values are from another table. So I have to add them later somehow. Can someone tell me how to do this?

mitarbeiter.Where(x => x.Id == 1).Select(x => new MitarbeiterDataModel { Gelegenheiten = 2 });

'Gelegenheiten' means opportunities and is present in my dataModel which is MitarbeiterDataModel . I tried it somehow this way... No success though:(

Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
  • Why don't you join the tables? http://stackoverflow.com/questions/21051612/entity-framework-join-3-tables – J.S. Orris Sep 02 '16 at 01:24
  • Can I do this with assigning it to a propertie? The value of the second table hast to take the variable name of the viewmodel. Like it has to be something like this 'Gelegenheiten = 2'. It is important that the name is 'Gelegenheiten' – Tom el Safadi Sep 02 '16 at 01:26

2 Answers2

1

Like Jeff Orris said, you can join both tables, then create your viewModels like so :

UserContext context = new UserContext();

List<MitarbeiterDataModel> mitarbeiter = new List<MitarbeiterDataModel>();

var users = (
    from user in context.Users
    join opp in context.Opportunities on user.OppurtunityId equals opp.OppurtunityId
    select new MitarbeiterDataModel { 
        Id = user.Id, 
        Vorname = user.Vorname, 
        Nachname = user.Nachname, 
        Studio = user.Studio,
        Gelegenheiten = opp.SomeField
        })
    .ToList();

mitarbeiter.AddRange(users);

Of course, i don't know the content of your classes, so I made up some fields.

Kinetic
  • 2,640
  • 17
  • 35
0

For the Legacy data I think you can use the SQL vernier to handle it, like

declare @Opportunitiesint

declare updatetrigger cursor for select Opportunities from Gelegenheiten where ***

open updatetrigger fetch next from updatetrigger into @Opportunities while @@fetch_status=0
begin

update employees set Opportunities =@Opportunities where ***

fetch next from updatetrigger into @Opportunities end
deallocate updatetrigger

Jia Lei
  • 11
  • 2