C
C#14mo ago
SWEETPONY

❔ ✅ may someone help me to rewrite this in linq?

var map = new Dictionary<string, List<NotificationModel>>();

foreach (var notification in confirmedNotifications)
{
var to = notification.To
.Except(new HashSet<string> { currentOperator.Name })
.ToList();

if (!to.Any()) continue;

foreach (var user in to)
{
if (map.ContainsKey(user))
map[user].Add(notification);
else
map[user] = new List<NotificationModel> { notification };
}
}
var map = new Dictionary<string, List<NotificationModel>>();

foreach (var notification in confirmedNotifications)
{
var to = notification.To
.Except(new HashSet<string> { currentOperator.Name })
.ToList();

if (!to.Any()) continue;

foreach (var user in to)
{
if (map.ContainsKey(user))
map[user].Add(notification);
else
map[user] = new List<NotificationModel> { notification };
}
}
I wrote only this:
var test = confirmedNotifications
.Where(notification => notification.To
.Except(new HashSet<string> { currentOperator.Name })
.Any());
var test = confirmedNotifications
.Where(notification => notification.To
.Except(new HashSet<string> { currentOperator.Name })
.Any());
3 Replies
reflectronic
reflectronic14mo ago
var res = confirmedNotifications
.SelectMany(notification => notification.To, (notification, to) => (notification, to))
.Where(t => t.to != currentOperator.Name)
.ToLookup(t => t.to, t => t.notification);
var res = confirmedNotifications
.SelectMany(notification => notification.To, (notification, to) => (notification, to))
.Where(t => t.to != currentOperator.Name)
.ToLookup(t => t.to, t => t.notification);
how did i do i like this question, actually, it took me a minute
SWEETPONY
SWEETPONYOP14mo ago
thanks!
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server