1

What I have is a binding list:

BindingList<partner> partnerList = _context.partner.Local.ToBindingList();

this._view.PartnerDatasource.DataSource = partnerList;

This is a datasource for a drop down menu. What I want is to put a specific item to show as first item in a drop down. I tried something like this:

 public  void Swap<T>(IList<T> list, int indexA, int indexB)
        {
            T tmp = list[indexA];
            list[indexA] = list[indexB];
            list[indexB] = tmp;
        }

and then:

 this.Swap(partnerList, 0, partnerList.Count - 1);

This works when it comes to swapping, but it somehow messes up entity framework completely and I get various errors further on when I try to use these entities (partner)...

What would an appropriate way to do the this?

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • "messes up entity framework completely and I get various errors further on when I try to use these entities" - How so? – Enigmativity Oct 04 '18 at 07:01
  • @Enigmativity Have no idea. Just too many errors, like "Adding a relationship with an entity which is in the Deleted state is not allowed" and few more... That Swap function messes it. – Whirlwind Oct 04 '18 at 07:03
  • When do the errors happen? – Han Oct 04 '18 at 07:06
  • When I try to add another entity which is in relation with partner entity, to the context. @Han – Whirlwind Oct 04 '18 at 07:07
  • What if you just reorder the partnerList instead of swapping its elements? – Han Oct 04 '18 at 07:09
  • @Han Let me try – Whirlwind Oct 04 '18 at 07:09
  • @Han I used Reverse() to change the order, and everything works fine. – Whirlwind Oct 04 '18 at 07:11
  • I think you could use LINQ to reorder the list if you want it in some pattern. – Han Oct 04 '18 at 07:12
  • 1
    Why do you swap elements in the list? It is the underlying data within the database and swapping any elements doesn't have any positive effect on the database. If you need to sort or re-order things for the user, you should do this in your view and not in the underlying data. – Oliver Oct 04 '18 at 07:13
  • @Oliver Cause I thought it is better idea to do it there, rather than re-ordering a view (drop down). Also didn't know it can affect on anything, just because of re-ordering the list... :( – Whirlwind Oct 04 '18 at 07:16
  • @Whirlwind - There's nothing that I see in your code that would produce the kinds of errors that you're getting. You need to provide us with a [mcve]. – Enigmativity Oct 04 '18 at 07:19
  • @Whirlwind I added a live demo to my answer. please check – Ashkan Mobayen Khiabani Oct 04 '18 at 07:29
  • The `ToBindingList()` method returns something that is derived from [`SortableBindingList()`](https://github.com/aspnet/EntityFramework6/blob/22a11d132306c257435fa7d6d75a31359cbbd05a/src/EntityFramework/Internal/ObservableBackedBindingList%60.cs). So in your view you could call [`ApplySort()`](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.ibindinglist.applysort) to sort the items as needed. I think the root cause is the implicit remove of the aItem in your swap method. – Oliver Oct 04 '18 at 07:33

3 Answers3

3

Give them priority and then order by it:

partnerList.OrderByDesending(x=> x.someProperty == Something); 

Or if you need to order by index:

  partnerList.Select((item ,i) => new { item , neworder = i == index ? 0 : 1})
           .OrderBy(x=> x.neworder).Select(a=> a.item); 

Live Demo

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
3
var names = new [] { "Alice", "Bob", "Charlie", "Dave", "Eve" };
var specialName = "Eve";

var sortedNames = names.OrderByDescending(x => x == specialName);
foreach (var name in sortedNames)
    Console.WriteLine(name);

Result:

Eve
Alice
Bob
Charlie
Dave
Han
  • 3,052
  • 2
  • 22
  • 31
  • 1
    So what I did is ordered stuff as you said, and then ` this._view.PartnerDatasource.DataSource = new BindingList(result.ToList());` to convert the result to binding list. It seems to be working... – Whirlwind Oct 04 '18 at 07:51
0

List does not implement the Move(Int32 oldIndex, Int32 newIndex) Method but an ObservableCollection does.

So what you can do is, first convert that list to an ObservableCollection and try using the .Move(Int32 oldIndex, Int32 newIndex) method.

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {

        List<Partner> partenrs=new List<Partner>();
        partenrs.Add(new Partner(){Name="A"});
        partenrs.Add(new Partner(){Name="B"});
        partenrs.Add(new Partner(){Name="C"});
        partenrs.Add(new Partner(){Name="D"});
        partenrs.Add(new Partner(){Name="E"});
        partenrs.Add(new Partner(){Name="F"});

        var obser=new ObservableCollection<Partner>(partenrs);

        obser.Move(0,5);
        foreach(var x in obser)
        {
            Console.WriteLine(x.Name);
        }
    }
}

class Partner
{
    public string Name{get;set;}
}
A_Sk
  • 4,532
  • 3
  • 27
  • 51