C@NYON⭐ONL!NE
C@NYON⭐ONL!NE
CC#
Created by C@NYON⭐ONL!NE on 2/10/2025 in #help
✅ LINQ help: IQueryable<Entity> -> Dictionary<string, Entity[]>
Hi, all: I'm looking for some help in writing a LINQ query. I'm interested in creating a dictionary where the keys are LogDatum.SerialNumber (nullable string), and the values are an array of the hourly maximum of LogDatum.Current (nullable double). This means I'm looking for an array whose elements are the maximum LogDatum of a certain hour. A sample of a dictionary I'm looking for:
Dictionary<string, LogDatum[]> myDict = new() {
{
"serial 1",
[
<LogDatum with max Current at "2025-01-01 09">,
<LogDatum with max Current at "2025-01-01 10">,
<LogDatum with max Current at "2025-01-01 11">,
<LogDatum with max Current at "2025-01-01 13">,
<LogDatum with max Current at "2025-01-01 14">,
]
},
};
Dictionary<string, LogDatum[]> myDict = new() {
{
"serial 1",
[
<LogDatum with max Current at "2025-01-01 09">,
<LogDatum with max Current at "2025-01-01 10">,
<LogDatum with max Current at "2025-01-01 11">,
<LogDatum with max Current at "2025-01-01 13">,
<LogDatum with max Current at "2025-01-01 14">,
]
},
};
My entity (generated by EF scaffolding) has an abbreviated definition of:
public class LogDatum
{
public long? Pk { get; set; }
public string? SerialNumber { get; set; }
public DateTime? Timestamp { get; set; }
public double? Current { get; set; }
}
public class LogDatum
{
public long? Pk { get; set; }
public string? SerialNumber { get; set; }
public DateTime? Timestamp { get; set; }
public double? Current { get; set; }
}
I have a query that starts to get me there:
var res = await _nktContext.LogData
.Where(data => data.Timestamp != null && data.Timestamp.Value >= dateStart && data.Timestamp.Value <= dateEnd)
.GroupBy(data => new { data.SerialNumber, Hour = data.Timestamp!.Value.ToString("yyyy-MM-dd HH") })
.ToDictionaryAsync(
grouping => grouping.Key.SerialNumber ?? "N/A",
grouping => grouping.ToArray()
);
var res = await _nktContext.LogData
.Where(data => data.Timestamp != null && data.Timestamp.Value >= dateStart && data.Timestamp.Value <= dateEnd)
.GroupBy(data => new { data.SerialNumber, Hour = data.Timestamp!.Value.ToString("yyyy-MM-dd HH") })
.ToDictionaryAsync(
grouping => grouping.Key.SerialNumber ?? "N/A",
grouping => grouping.ToArray()
);
However this gets me all the grouped by an hour, which of course is caused by grouping => grouping.ToArray(), but I'm not clever enough to get the rest of the way there. Any help or tips appreciated, thanks!
3 replies
CC#
Created by C@NYON⭐ONL!NE on 2/6/2025 in #help
✅ Will I fall into a gotcha with this constructor?
Hi, all: I'm trying to setup a class for managing my AWS S3 interactions, but I'm wondering if there are any issues with my definition:
C#
public class S3Service(ILogger<S3Service> logger, IAmazonS3 s3Client) : IS3Service
{
private readonly ILogger<S3Service> _logger = logger;
private readonly IAmazonS3 _s3Client = s3Client;
private readonly TransferUtility _transferUtility = new(s3Client);
...
C#
public class S3Service(ILogger<S3Service> logger, IAmazonS3 s3Client) : IS3Service
{
private readonly ILogger<S3Service> _logger = logger;
private readonly IAmazonS3 _s3Client = s3Client;
private readonly TransferUtility _transferUtility = new(s3Client);
...
Looks simple enough, but I'm worried about two things, TransferUtility inherits from IDisposable, will the framework properly handle the disposability of the _transferUtility? Also, would it be more appropriate to create a new TransferUtility object under the scope of each function that I want to use the class rather than creating a class field? For example:
C#
public Task MyUploadFunctionAsync()
{
using var transfer = new TransferUtility();
... transfer files
}
C#
public Task MyUploadFunctionAsync()
{
using var transfer = new TransferUtility();
... transfer files
}
I'll be expecting to upload many (~50 25MB) files at once. If the latter approach is more appropriate I can of course pass in a list of objects I'd like to upload. Thanks 🙂
25 replies