c# - how can I swap two child entity values -
i have entity called "space" , inside has "images". images has ordering property int. i'm trying see if there easy linq can made swap ordering values.
public class space { public int spaceid { get; set; } public virtual icollection<spaceimage> images { get; set; } } public class spaceimage { public int spaceimageid { get; set; } public byte[] image { get; set; } public byte[] imagethumbnail { get; set; } public string contenttype { get; set; } public int ordering { get; set; } }
ex. image might have ordering 3 , have 6, want swap these 2 numbers each of these images
public void swap(int spaceid, int old, int new) { //swap 3 , 6 ordering value 2 spaceimages contain these values spaceid spaceid }
in general, linq used querying. use find appropriate spaceimage
instances, set values appropriately:
// assuming method in space class public void swap(int oldorder, int neworder) { var oldinst = this.images.firstordefault(si => si.ordering == oldorder); var newinst = this.images.firstordefault(si => si.ordering == neworder); if (oldinst != null && newinst != null) { oldinst.ordering = neworder; newinst.ordering = oldorder; } else { // there weren't matching images - handle case here } }
Comments
Post a Comment