I'm looking to add a notification record to my users with an SQL database using Entity Framework and LINQ. My users already have a nested array of tags that they have chosen. I would like to add a notification record to a user if their chosen tags contain any tag that is included in the array tags i pass into my method.
This is what my Tag array looks like:
[
{
"Id": 2,
"Text": "Blue",
"Value": "blue"
},
{
"Id": 4,
"Text": "Red",
"Value": "red"
},
{
"Id": 3,
"Text": "White",
"Value": "white"
}
]
My Notification looks like this:
{
"Image": "https://www.image.com",
"Heading": "Example heading",
"Content": "Example content"
}
My users have a nested array of tags associated with them that also looks like tags array above and have the same id's.
My method is currently like this:
AddUserNotification(Notification notification, IList<Tag> tags)
{
var usersToAddTheNotificationTo = DbContext.Users
.Where(User.Tags.Contains(tags)).ToList();
foreach(var user in usersToAddTheNotificationTo)
{
user.Notifications.Add(notification);
}
}
How can I achieve this is the most simple way?
.Where(user => user.Tags.Any(tag => tags.Contains(tag)))