score:3
Accepted answer
//asuming your db has these fields: db.shifts, db.operatorshifts
//and model is the current operator
var existingshifts = (from os in db.operatorshifts
where os.operatorid == model.id
select os).tolist();
ienumerable<guid> newshiftids = ?; //don't now how your selected shift ids got post back, figure it out yourself
var shiftstoremove = existingshifts.where(e => newshiftids.all(id => e.id != id)).tolist();
var shiftidstoappend = newshiftids.where(id => exising.all(e => e.id != id)).tolist();
foreach(var shift in shiftstoremove)
{
db.operatorshifts.remove(shift);
}
foreach(var shiftid in shiftidstoappend)
{
db.operatorshifts.add(new operatorshift{
operatorid = model.id,
shiftid = shiftid
});
}
Source: stackoverflow.com
Related Query
- Using LINQ and EF, how to remove values from database where not in list of items
- How to get items from SQL, using linq to sql, where Date is from today to 30 days ahead and return as a list
- How do I remove items from generic list, based on multiple conditions and using linq
- Get items from list where index not equal to an int using LINQ
- Using LINQ to remove items from a list that do not apear in another list
- Remove items from list using their IDs and linq
- How to populate a DropDownList from a SQL database using linq and only select Distinct Values
- How to remove items from multidimensional List using LINQ
- How to generate an index dictionary from a list of items in C# using linq and lambda
- How to select values within a provided index range from a List using LINQ
- compare two list and return not matching items using linq
- Remove items from list that intersect on property using Linq
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- linq remove items from query where any list value is present
- How to generate a unique list of items from another list using LINQ in C#
- Select items from a List where the children contain the items from another List using LINQ
- Using LINQ to get a list of items where the item contains a part of an item from another list
- Is a full list returned first and then filtered when using linq to sql to filter data from a database or just the filtered list?
- How should i use group by and sum on different values of list of models using linq
- Get items from input list that do not exist in the database using EF Core 2.1
- Get Min and Max for items from database using Linq
- How to remove substring from all strings in a list in C# using LINQ
- How to iterate a list returned from a Dictionary using Linq and C#
- How to return list of items from linq to sql query and call it in a console application
- Remove items from list where not at *all* locations
- Group and Merge multiple values from single List using LINQ
- Get a strongly typed list of unique items using LINQ GroupBy() from database
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- Not able to select values from right list using LINQ join
- How to retrieve the values from list using indexes by linq query?
More Query from same tag
- Convert Object Search Results into Function Class Values
- Descendants() with a parameter returns empty result
- Passing different values to the SELECT NEW part of a LINQ query
- LINQ to SQL Insert Sequential GUID
- Convert object collection to dictionary using LINQ
- Linq Select Statement wont work
- LINQ: Concatenate/Union multiple sequences
- Linq query construction based on empty parameters
- Grouping and sorting of elements
- How do I check composite ID exists?
- How to resolve object reference error in lambda expression of linq in c#?
- convert linq to object query to sql query (no linq to sql code or datacontext)
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- Most efficent way to do where search with unknown amount of variables to match with EF linq?
- Unable to change foreach to LINQ syntax
- LINQ to SQL and Concurrency Issues
- how to retrieve rows from database selected from dropdown list in mvc 4
- Why XDocument isn't reading elements value?
- IQueryable - how to get all unique Years from db set?
- If user searches for "OConnors", how to get both "Niall O'Connors" & "Niall OConnors"?
- LINQ Additional Filter in Select Statement
- LINQ - GroupBy Year/Month
- Dynamic lambda wrapped with try catch
- Using QueryString to display Details
- How to join multiple documents?
- How to retrieve Users with a specific role via Linq in ASPnet Identity
- List Tuple DateTime
- Why ExpandoObject doesn't work properly with Guid converted to string?
- Using .Any from System.Linq on array to find a string
- How to populate IEnumerable in Lambda expression?