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 Query
- 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
More Query from same tag
- Convert SQL Query to LINQ Lambda C#
- Confused over access to private variable from Linq query
- LINQ to SQL: inner join with manual association on SQL 2000
- iEnumrable vs IQueryable in linq working issue
- How do i include more than one property in group by key?
- Performing a Parent then Child sort in Linq
- Time date conversion in linq
- LINQ query for a GroupBy and how this is displayed in razor view
- Is there any requirement for model class in SQLite database?
- LINQ list of sublist
- Instantiate empty IQueryable for use with Linq to sql
- How can I combine multiple refactored Select expressions in Linq (to EF or SQL)?
- Convert nested for each loop to LINQ
- Distinct Column in SelectList for dropdown list
- Linq for objects
- Get the lowest year from LINQ query results
- LINQ and web service cannot return anonymous types, and you cannot construct an object in a query?
- Does not contain definition for 'Contains'
- C# Linq Join not equal
- Need explanation about LINQ GroupJoin syntax
- Apply Where conditionally (OR between them) LINQ
- Get all remaining items with GetRange
- Intersect N SortedLists<T> in c#
- Lambda syntax in linq to db4o?
- EF Linq can't convert sql query to the right linq
- How to get top 5 products with highest views?
- How to make an EqualityComparer compare against two fields?
- Adding a WHERE clause to a Group Join
- Javascript (jquery) functionality similar to linq's Where()
- LINQ query with list <T> " Object reference not set to an instance of an object."