Home > Mobile >  Get LINQ query Result with column value based on DateTime column
Get LINQ query Result with column value based on DateTime column

Time:04-06

I have a table of machine details with one DateTime column in which I am entering the date and time of detail received for the corresponding machine.

Sample data is like this

MachineID  X   Y   Z   HealthRcvd
M1         X1  Y1  Z1  2022-04-04 10:20:04
M2         X2  Y2  Z2  2022-04-02 10:30:00
M3         X3  Y3  Z3  2022-04-04 10:04:20

Now what I want in output is that when I fetch the data using LINQ query, compare the value in HealthRcvd column and show text in one more column as 'Connected' (If its value is not more than 3 Hrs older) and 'Disconnected' (If date and time is older than 3 Hrs)

If a fetch data on 2022-04-04 11:00:00, I need output as

MachineID  X   Y   Z   Status       HealthRcvd
M1         X1  Y1  Z1  Connected    2022-04-04 10:20:04
M2         X2  Y2  Z2  Disconnected 2022-04-02 10:30:00
M3         X3  Y3  Z3  Connected    2022-04-04 10:04:20

CodePudding user response:

I am able to generate the required output data, so here I am sharing it if someone needs that

List<HealthResult> objResp = (from H in Table
                              select new SearchResult
                              {
                                  MachineID = H.MachineID,
                                  X = H.X,
                                  Y = H.Y,
                                  Z = H.Z,
                                  Status = (System.Data.Entity.SqlServer.SqlFunctions.DateDiff("minute", H.HealthRcvd, DateTime.Now) <= 30) ? "Connected" : "Disconnected",
                                  HealthRcvd = H.HealthRcvd
                              }).ToList();
  • Related