我有一个名为" Space"在里面它有"图像"。 Images具有int的排序属性。我试图看看是否有一个简单的linq可以交换订单值。
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; }
}
离。 一个图像可能有3个订单而另一个图像有6个,我想为这些图像中的每一个交换这两个数字
public void Swap(int spaceId, int old, int new)
{
//swap 3 and 6 for the ordering value for the two spaceImages that contain these values where spaceId is spaceId
}
答案 0 :(得分:3)
通常,LINQ将用于查询。您可以使用它来查找适当的SpaceImage
实例,然后适当地设置值:
// Assuming this is a method in the 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 that case here
}
}