✅ Is it possible to use grouping here?
I have following logic:
I would like to use
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 }) );
MessagesSend
like that:
await _messagingChannelsClient.MessagesSend(new(groupKey, messages) )
;
it is unpossible because of await in object. So how to fix it?6 Replies
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));
}
if await GetSubjectsAddresses is forbidden there just assign it to a variable as an intermediate step 🤔
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.
The 'await' expression can only be used in a method or lambda marked with the 'async' modifier
it is group by
so I can't do async
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));
you should have a another query first to recover all the data in one way or the other
thanks