0

I have to columns AppStart and Duration I wish to add the duration to the App Start but I am using linq.

I am using the following to get the appointments and that works fine but I need to return a new field in this called AppEnd which is based on the duration field which can be 15,25,30,60.

This is using linq and is asking how to add a new field within link based on another field.

List<Appointment> appointments = new List<Appointment>();

appointments= _sourceEntities.Appointments.Where(a=> a.ApptDate.Year == 
currentDate.Year && a.ApptDate.Month == currentDate.Month && a.ApptDate.Day 
== currentDate.Date.Day).ToList();

So my question is how would I create a new field AppEnd but also keep all the data intact from the above query ?.

NetMage
  • 26,163
  • 3
  • 34
  • 55
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • Possible duplicate of [Adding a Time to a DateTime in C#](https://stackoverflow.com/questions/2146296/adding-a-time-to-a-datetime-in-c-sharp) – Herohtar Dec 21 '17 at 21:55
  • Do you have `AppEnd` in the db? If not, do you have it in a C# class on the application side? If you do have a C# class, then once you have the data set the `AppEnd` property on the application side. If you do not have it, then you can project into an anonymous class. You need to provide more information. – CodingYoshi Dec 21 '17 at 21:56
  • @CodingYoshi No Class yoshi would that be the better way of doing that – c-sharp-and-swiftui-devni Dec 21 '17 at 21:57
  • 1
    Well you can select and project into an anonymous type and the calculation for the `AppEnd` can be done on the db side. However, anonymous types have limitations because their scope is limited to the method. Thus you cannot return the anonymous type from the method. If you want to return an object which has this property then you need to create a class with that property so you can return it from the method. – CodingYoshi Dec 21 '17 at 22:02
  • Which ORM (+ version) is this? Or: what's the base type of `_sourceEntities`? – Gert Arnold Dec 22 '17 at 07:45

2 Answers2

1

You need to use an anonymous type:

var appointments = _sourceEntities.Appointments
                                  .Where(a => a.ApptDate.Year == currentDate.Year &&
                                              a.ApptDate.Month == currentDate.Month &&
                                              a.ApptDate.Day == currentDate.Date.Day)
                                  .Select(a => new { a,
                                                     AppEnd = a.ApptTime.AddMinutes(a.Duration)
                                                    })
                                  .ToList();

Then you can access the existing Appointment fields with .a. and AppEnd with .AppEnd.

You can also project the individual fields in the new anonymous object:

                                  .Select(a => new { a.ApptDate,
                                                     a.ApptTime,
                                                     AppEnd = a.ApptTime.AddMinutes(a.Duration)
                                                    })
NetMage
  • 26,163
  • 3
  • 34
  • 55
0

for each item in the list...

AppEnd = ApptDate.Add(New TimeSpan(0, 60, 0));

EDIT RE: do it using LINQ.

Linq's usually just for reading stuff. However it's possible. I see you have to .ToList() already.

More info here for the curious: Update all objects in a collection using LINQ

Sample for you

_sourceEntities.Appointments.Where( a=> 
          a.AppEnd = a.ApptDate.Add(New TimeSpan(0, 60, 0)) ).ToList();

this just gives you all 60 minute appointments. Of course you can extend the logic from here to do what you want (for example put in a.Duration instead of 60 :-).

FastAl
  • 6,194
  • 2
  • 36
  • 60