score:3
Accepted answer
The code looks fine. As Paul said you should not use save twice. I would also be tempted to remove some duplication with the following
public void OrderUp(int favouriteId)
{
ChangeOrder(false, favouriteId);
}
public void OrderDown(int favouriteId)
{
ChangeOrder(true, favouriteId);
}
private void ChangeOrder(bool OrderDown, int favouriteId)
{
UsFavourite currentFav = db.UsFavourites.FirstOrDefault(m => m.FavouriteID == favouriteId);
// Get favourite by current favourite's order + 1
UsFavourite tempFav;
if (OrderDown)
{
tempFav = db.UsFavourites.OrderBy(m => m.FavouriteOrder).FirstOrDefault(m => m.FavouriteOrder > (currentFav.FavouriteOrder));
}
else
{
tempFav = db.UsFavourites.OrderByDescending(m => m.FavouriteOrder).FirstOrDefault(m => m.FavouriteOrder < (currentFav.FavouriteOrder));
}
if (tempFav != null)
{
//update tempFav
tempFav.FavouriteOrder = currentFav.FavouriteOrder;
//update currentFav
currentFav.FavouriteOrder = currentFav.FavouriteOrder + change;
Save();
}
}
Source: stackoverflow.com
Related Articles
- change order on my object list
- Order parent object list by child list attribute
- Change the order of List items conditionally with LINQ
- Group objects in List<T> by id and order the List by duplicateCount for each object
- C# LINQ Order List by Object property, Select X Amount
- Sort list and select object based on order by
- Linq extension. Change property value in source list
- Linq code to get the index of an object in an array from an object within a list
- How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?
- How to distribute a list of objects in a new reorganized list of objects having an embedded list in order to change the root primary key/index?
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- List or Array of String Contain specific word in Html Source Code
- How to order a C# custom object collection based upon a list
- Order dynamic list of object by common property with pagination
- Why is my code returning a list of the same object 4 times?
- Change the anonymous type to list of object in C#
- c# Linq or code to extract groups from a single list of source data
- Order List By Distinct And Object Property Larget value
- Order by list in complex object
- LINQ: Select an object and change some properties without creating a new object
- Create a list from two object lists with linq
- how to check if object already exists in a list
- C# - code to order by a property using the property name as a string
- ascending/descending in LINQ - can one change the order via parameter?
- LINQ: Select where object does not contain items from list
- Return list of specific property of object using linq
- Get index of object in a list using Linq
- Create a list of one object type from a list of another using Linq
- Mapping a list of object models onto another list using linq
- Linq select object from list depending on objects attribute
- Cannot add manual anonym items to IQueryable
- How to ignore 'null' in order by clause
- Nested foreach to change list items
- LINQ, select from multi table
- Avoiding Redundancies in XML documents
- Query with many-to-many relationship by linQ and filter result/ Get dynamic data by LINQ
- how to optimize linq query where Data is coming from two different databases?
- Sort items arbitrarily using external priority list
- Linq To Sql Get data into Label
- Will First() perform the OrderBy()?
- C# LINQ make any take 2 or more from array
- Linq Join by ids and sort by name
- changing the database before every test
- Merge two c# object(json) lists as parent child
- Linq returning multiple values in a group by, need only last modified
- LINQ query the most recent records
- Select amount from one table by
- Returning list that satisfy conditions
- EF1 SelectMany() left join
- How do I split authors into a XML list