C
C#12mo ago
SWEETPONY

✅ Is it possible to use grouping here?

I have following logic:
foreach (var argument in arguments.Items)
{
var messageDto = new MessageDto
{
Timestamp = DateTimeOffset.Now,
Title = "", // think about this
Content = argument.Event.ToJsonString(), // fix this
Behavior = argument.Behavior,
To = await GetSubjectsAddresses(
channelName: argument.ChannelName,
subjects: subjectsQueryResponse
.Where(subjectDto => argument.SubjectIds
.Contains(subjectDto.Id)),
channelSettings: channelSettings)
};

await _messagingChannelsClient.MessagesSend(new(argument.ChannelName, new[] { messageDto }) );
foreach (var argument in arguments.Items)
{
var messageDto = new MessageDto
{
Timestamp = DateTimeOffset.Now,
Title = "", // think about this
Content = argument.Event.ToJsonString(), // fix this
Behavior = argument.Behavior,
To = await GetSubjectsAddresses(
channelName: argument.ChannelName,
subjects: subjectsQueryResponse
.Where(subjectDto => argument.SubjectIds
.Contains(subjectDto.Id)),
channelSettings: channelSettings)
};

await _messagingChannelsClient.MessagesSend(new(argument.ChannelName, new[] { messageDto }) );
I would like to use MessagesSend like that: await _messagingChannelsClient.MessagesSend(new(groupKey, messages) ); it is unpossible because of await in object. So how to fix it?
6 Replies
SWEETPONY
SWEETPONYOP12mo ago
I have this function, it looks good because I don't have await in To
private async Task OnMessagesSendTargets(
IDeliveryHandlerContext context,
MessagesSendTargetsArguments arguments)
{
var groupedMessages = arguments.Items
.GroupBy(
targetDto => targetDto.ChannelName,
targetDto => new MessageDto
{
Timestamp = DateTimeOffset.Now,
To = targetDto.To,
Content = targetDto.Message,
Behavior = targetDto.Behavior
} )
.ToDictionary(
group => group.Key,
group => group.ToList());

foreach (var (name, messages) in groupedMessages)
await _messagingChannelsClient.MessagesSend(new(name, messages));
}
private async Task OnMessagesSendTargets(
IDeliveryHandlerContext context,
MessagesSendTargetsArguments arguments)
{
var groupedMessages = arguments.Items
.GroupBy(
targetDto => targetDto.ChannelName,
targetDto => new MessageDto
{
Timestamp = DateTimeOffset.Now,
To = targetDto.To,
Content = targetDto.Message,
Behavior = targetDto.Behavior
} )
.ToDictionary(
group => group.Key,
group => group.ToList());

foreach (var (name, messages) in groupedMessages)
await _messagingChannelsClient.MessagesSend(new(name, messages));
}
Omnissiah
Omnissiah12mo ago
if await GetSubjectsAddresses is forbidden there just assign it to a variable as an intermediate step 🤔
MODiX
MODiX12mo ago
Zeth
REPL Result: Success
var c = new C() {
L = await Task.FromResult(4)
};
class C { public int L { get; set; } }
var c = new C() {
L = await Task.FromResult(4)
};
class C { public int L { get; set; } }
Compile: 506.325ms | Execution: 73.719ms | React with ❌ to remove this embed.
SWEETPONY
SWEETPONYOP12mo ago
The 'await' expression can only be used in a method or lambda marked with the 'async' modifier

var groupedMessages = arguments.Items
.GroupBy(
targetDto => targetDto.ChannelName,
targetDto => new MessageDto
{
Timestamp = DateTimeOffset.Now,
Title = "", // think about this
Content = targetDto.Event.ToJsonString(), // fix this
Behavior = targetDto.Behavior,
To = await GetSubjectsAddresses(
channelName: targetDto.ChannelName,
subjects: subjectsQueryResponse
.Where( subjectDto => targetDto.SubjectIds
.Contains( subjectDto.Id ) ),
channelSettings: channelSettings)
} )
.ToDictionary(
group => group.Key,
group => group.ToList());

foreach (var (name, messages) in groupedMessages)
await _messagingChannelsClient.MessagesSend(new(name, messages));

var groupedMessages = arguments.Items
.GroupBy(
targetDto => targetDto.ChannelName,
targetDto => new MessageDto
{
Timestamp = DateTimeOffset.Now,
Title = "", // think about this
Content = targetDto.Event.ToJsonString(), // fix this
Behavior = targetDto.Behavior,
To = await GetSubjectsAddresses(
channelName: targetDto.ChannelName,
subjects: subjectsQueryResponse
.Where( subjectDto => targetDto.SubjectIds
.Contains( subjectDto.Id ) ),
channelSettings: channelSettings)
} )
.ToDictionary(
group => group.Key,
group => group.ToList());

foreach (var (name, messages) in groupedMessages)
await _messagingChannelsClient.MessagesSend(new(name, messages));
it is group by so I can't do async
Omnissiah
Omnissiah12mo ago
you should have a another query first to recover all the data in one way or the other
SWEETPONY
SWEETPONYOP12mo ago
thanks
Want results from more Discord servers?
Add your server