Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将IEnumerable转化为可观察集合_C#_Wpf - Fatal编程技术网

C# 将IEnumerable转化为可观察集合

C# 将IEnumerable转化为可观察集合,c#,wpf,C#,Wpf,我有这个数不清的 public static IEnumerable<Shipments> Shipments { get { var hash = new HashSet<Routes>(new ShipmentsComparer()); // rename ShipmentsComparer cause it is actually RoutesComparer foreach (var ite

我有这个数不清的

public static IEnumerable<Shipments> Shipments
{
    get
    {
        var hash = new HashSet<Routes>(new ShipmentsComparer()); // rename ShipmentsComparer cause it is actually RoutesComparer             
        foreach (var item in Loads)
        {
            if (item.ShipTo.Contains(" "))
            {
                foreach (var item2 in Routes.Where(d => d.DockCode == item.ShipTo.Substring(0, item.ShipTo.IndexOf(" ")) && d.CarrierDeparture.TimeOfDay == item.ShipTime.TimeOfDay))
                {
                    if (hash.Add(item2))
                    { 

                        yield return new Shipments { Arrival = item2.CarrierArrival, Departure = item2.CarrierDeparture, Issuer = item.Customer, Destination = item.ShipTo, LoadType = item.LoadType };
                    }
                }
            }
        }
    }
}
将其转换为一个
可观察的集合

public static ObservableCollection<Routes> Routes { get; set; } = new ObservableCollection<Routes>();

public static ObservableCollection<Loads> Loads { get; set; } = new ObservableCollection<Loads>();
目前,我正在使用此IEnumerable填充
数据网格
,我无法继续以这种方式使用它,因为当数据网格刷新时,它会删除用户选择的排序

编辑:

类别装运

public class Shipments : BaseClass
    {
        private DateTime _Arrival;
        public DateTime Arrival
        {
            get { return _Arrival; }
            set { _Arrival = value; RaisePropertyChanged(nameof(Arrival)); }
        }

        private DateTime _Departure;
        public DateTime Departure
        {
            get { return _Departure; }
            set { _Departure = value; RaisePropertyChanged(nameof(Departure)); }
        }

        private string _Issuer;
        public string Issuer
        {
            get { return _Issuer; }
            set { _Issuer = value; RaisePropertyChanged(nameof(Issuer)); }
        }

        private string _Destination;
        public string Destination
        {
            get { return _Destination; }
            set { _Destination = value; RaisePropertyChanged(nameof(Destination)); }
        }

        private string _LoadType;
        public string LoadType
        {

            get
            {
                _LoadType = (Departure - Arrival).ToString();
                if (_LoadType == "00:30:00")
                {
                    _LoadType = "Drop Hook";
                }
                else
                {
                    _LoadType = "Live Load";
                }
                return _LoadType.ToString();
            }
            set
            {
                _LoadType = value;
            }
        }
比较器

class ShipmentsComparer : IEqualityComparer<Routes>
    {
        public bool Equals(Routes route1, Routes route2) =>
            route1.DockCode == route2.DockCode && route1.CarrierArrival.Date == route2.CarrierArrival.Date &&
            route1.CarrierArrival.Hour == route2.CarrierArrival.Hour && route1.CarrierArrival.Minute == route2.CarrierArrival.Minute;


        public int GetHashCode(Routes obj) =>
        obj.DockCode.GetHashCode() ^ (obj.CarrierArrival.GetHashCode() * 13);
    }
public class ShipmentsComparer : IEqualityComparer<Shipments>
{
    public bool Equals(Shipments left, Shipments right)
    {
        if (left == null) return right == null;

        return left.Arrival == right.Arrival &&
               left.Departure == right.Departure &&
               left.Issuer == right.Issuer &&
               left.Destination == right.Destination &&
               left.LoadType == right.LoadType;

    }

    public int GetHashCode(Shipments shipments)
    {
        int hashCode = 376624599;
        hashCode = hashCode * -1521134295 + shipments.Arrival.GetHashCode();
        hashCode = hashCode * -1521134295 + shipments.Departure.GetHashCode();
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.Issuer);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.Destination);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.LoadType);
        return hashCode;
    }
}
public class RoutesComparer : IEqualityComparer<Routes>
{
    public bool Equals(Routes route1, Routes route2) =>
        route1.DockCode == route2.DockCode && route1.CarrierArrival.Date == route2.CarrierArrival.Date &&
        route1.CarrierArrival.Hour == route2.CarrierArrival.Hour && route1.CarrierArrival.Minute == route2.CarrierArrival.Minute;


    public int GetHashCode(Routes obj) =>
    obj.DockCode.GetHashCode() ^ (obj.CarrierArrival.GetHashCode() * 13);
}

public class Routes
{
    public object DockCode { get; internal set; }
    public DateTime CarrierArrival { get; internal set; }
}
目前我使用上面的
IEnumerable


我希望它是一个
可观察集合
而不是
IEnumerable
,以便轻松刷新
数据网格
,并保持项目过滤。

实现本主题中的一个简单示例

班级人员

public class Person : IEquatable<Person>
{
    public string FirstName { get; }
    public string LastName { get; }

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as Person);
    }

    public bool Equals(Person other)
    {
        return other != null &&
               FirstName == other.FirstName &&
               LastName == other.LastName;
    }

    public override int GetHashCode()
    {
        int hashCode = 1938039292;
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FirstName);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(LastName);
        return hashCode;
    }
}
公共类人员:IEquatable
{
公共字符串名{get;}
公共字符串LastName{get;}
公众人物(字符串名、字符串名)
{
名字=名字;
LastName=LastName;
}
公共覆盖布尔等于(对象对象对象)
{
回报等于(obj为个人);
}
公共布尔等于(其他人)
{
返回其他!=null&&
FirstName==other.FirstName&&
LastName==other.LastName;
}
公共覆盖int GetHashCode()
{
int hashCode=1938039292;
hashCode=hashCode*-1521134295+EqualityComparer.Default.GetHashCode(FirstName);
hashCode=hashCode*-1521134295+EqualityComparer.Default.GetHashCode(LastName);
返回哈希码;
}
}
过滤后的

    public ObservableCollection<Person> People1 { get; } = new ObservableCollection<Person>()
        {
            new Person("Donald", "Duck"),
            new Person("Daisy", "Duck"),
            new Person("Jack", "Daniels")
        };

    public ObservableCollection<Person> People2 { get; } = new ObservableCollection<Person>()
        {
            new Person("Donald", "Duck"),
            new Person("Daisy", "Duck"),
            new Person("Jim", "Beam")
        };

    public ObservableCollection<Person> PeopleInBothCollections { get; } = new ObservableCollection<Person>();

    public void RenderPeople()
    {
        List<Person> people = People1.Where(prs => People2.Contains(prs))
            .ToList();


        for (int i = PeopleInBothCollections.Count-1; i >=0 ; i--)
        {
            if (!people.Contains(PeopleInBothCollections[i]))
                PeopleInBothCollections.RemoveAt(i);
        }

        foreach (Person person in people)
        {
            if (!PeopleInBothCollections.Contains(person))
                PeopleInBothCollections.Add(person);
        }
    }
    public ObservableCollection<Routes> Loads { get; } = new ObservableCollection<Routes>();

    public ObservableCollection<Routes> Routes { get; } = new ObservableCollection<Routes>();

    public ObservableCollection<Shipments> ShipmentsColl { get; } = new ObservableCollection<Shipments>();

    public void RenderRoutes()
    {
        var hashRoutes = new HashSet<Routes>(new RoutesComparer());
        HashSet<Shipments> hashShipments = new HashSet<Shipments>(new ShipmentsComparer());
        foreach (var item in Loads)
        {
            if (item.ShipTo.Contains(" "))
            {
                foreach (var item2 in Routes.Where(d => d.DockCode == item.ShipTo.Substring(0, item.ShipTo.IndexOf(" ")) && d.CarrierDeparture.TimeOfDay == item.ShipTime.TimeOfDay))
                {
                    if (hashRoutes.Add(item2))
                    {
                        // Forming a collection of results
                        hashShipments.Add( new Shipments { Arrival = item2.CarrierArrival, Departure = item2.CarrierDeparture, Issuer = item.Customer, Destination = item.ShipTo, LoadType = item.LoadType });
                    }
                }
            }
        }


        // We bring in line the public collection of the resulting collection of results.
        for (int i = ShipmentsColl.Count-1; i >=0; i--)
        {
            if (hashShipments.Contains(ShipmentsColl[i]))
                hashShipments.Remove(ShipmentsColl[i]);
            else
                ShipmentsColl.RemoveAt(i);
        }

        // Add missing results.
        foreach (Shipments shipments in hashShipments)
        {
            ShipmentsColl.Add(shipments);
        }
    }
public observetecollection People1{get;}=new observetecollection()
{
新人(“唐老鸭”),
新人(“黛西”、“鸭子”),
新人(“杰克”、“丹尼尔斯”)
};
public observetecollection People2{get;}=new observetecollection()
{
新人(“唐老鸭”),
新人(“黛西”、“鸭子”),
新人(“吉姆”,“梁”)
};
public ObservableCollection PeopleInBothCollections{get;}=new ObservableCollection();
公共void RenderPeople()
{
列出people=People1.Where(prs=>People2.Contains(prs))
.ToList();
对于(int i=PeopleInBothCollections.Count-1;i>=0;i--)
{
如果(!people.Contains(PeopleInBothCollections[i]))
两个集合中的人。移除(i);
}
foreach(人对人)
{
如果(!PeopleInBothCollections.Contains(person))
PeopleInBothCollections.Add(个人);
}
}

我查看了您对主题问题所做的编辑。 但我仍然不了解问题的许多细节,因此我无法提供完全适合答案的实现:
加载元素的类型是什么?
什么是对现有装运的更改,什么是新装运实例

装运集合属性的非常不幸的名称与此集合的项目类型名称相同

就我对你的任务的理解而言,这里有一个选择。 但我不能保证它会完全适合你

发货比较人

class ShipmentsComparer : IEqualityComparer<Routes>
    {
        public bool Equals(Routes route1, Routes route2) =>
            route1.DockCode == route2.DockCode && route1.CarrierArrival.Date == route2.CarrierArrival.Date &&
            route1.CarrierArrival.Hour == route2.CarrierArrival.Hour && route1.CarrierArrival.Minute == route2.CarrierArrival.Minute;


        public int GetHashCode(Routes obj) =>
        obj.DockCode.GetHashCode() ^ (obj.CarrierArrival.GetHashCode() * 13);
    }
public class ShipmentsComparer : IEqualityComparer<Shipments>
{
    public bool Equals(Shipments left, Shipments right)
    {
        if (left == null) return right == null;

        return left.Arrival == right.Arrival &&
               left.Departure == right.Departure &&
               left.Issuer == right.Issuer &&
               left.Destination == right.Destination &&
               left.LoadType == right.LoadType;

    }

    public int GetHashCode(Shipments shipments)
    {
        int hashCode = 376624599;
        hashCode = hashCode * -1521134295 + shipments.Arrival.GetHashCode();
        hashCode = hashCode * -1521134295 + shipments.Departure.GetHashCode();
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.Issuer);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.Destination);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.LoadType);
        return hashCode;
    }
}
public class RoutesComparer : IEqualityComparer<Routes>
{
    public bool Equals(Routes route1, Routes route2) =>
        route1.DockCode == route2.DockCode && route1.CarrierArrival.Date == route2.CarrierArrival.Date &&
        route1.CarrierArrival.Hour == route2.CarrierArrival.Hour && route1.CarrierArrival.Minute == route2.CarrierArrival.Minute;


    public int GetHashCode(Routes obj) =>
    obj.DockCode.GetHashCode() ^ (obj.CarrierArrival.GetHashCode() * 13);
}

public class Routes
{
    public object DockCode { get; internal set; }
    public DateTime CarrierArrival { get; internal set; }
}
公共类装运比较人:IEqualityComparer
{
公共布尔等于(左装运、右装运)
{
if(left==null)返回right==null;
返回左侧。到达==右侧。到达&&
左。出发==右。出发&&
left.Issuer==right.Issuer&&
left.Destination==right.Destination&&
left.LoadType==right.LoadType;
}
public int GetHashCode(装运)
{
int hashCode=376624599;
hashCode=hashCode*-1521134295+装运。到达。GetHashCode();
hashCode=hashCode*-1521134295+装运。出发。GetHashCode();
hashCode=hashCode*-1521134295+EqualityComparer.Default.GetHashCode(shippings.Issuer);
hashCode=hashCode*-1521134295+EqualityComparer.Default.GetHashCode(装运.目的地);
hashCode=hashCode*-1521134295+EqualityComparer.Default.GetHashCode(shippings.LoadType);
返回哈希码;
}
}
路由比较程序

class ShipmentsComparer : IEqualityComparer<Routes>
    {
        public bool Equals(Routes route1, Routes route2) =>
            route1.DockCode == route2.DockCode && route1.CarrierArrival.Date == route2.CarrierArrival.Date &&
            route1.CarrierArrival.Hour == route2.CarrierArrival.Hour && route1.CarrierArrival.Minute == route2.CarrierArrival.Minute;


        public int GetHashCode(Routes obj) =>
        obj.DockCode.GetHashCode() ^ (obj.CarrierArrival.GetHashCode() * 13);
    }
public class ShipmentsComparer : IEqualityComparer<Shipments>
{
    public bool Equals(Shipments left, Shipments right)
    {
        if (left == null) return right == null;

        return left.Arrival == right.Arrival &&
               left.Departure == right.Departure &&
               left.Issuer == right.Issuer &&
               left.Destination == right.Destination &&
               left.LoadType == right.LoadType;

    }

    public int GetHashCode(Shipments shipments)
    {
        int hashCode = 376624599;
        hashCode = hashCode * -1521134295 + shipments.Arrival.GetHashCode();
        hashCode = hashCode * -1521134295 + shipments.Departure.GetHashCode();
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.Issuer);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.Destination);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(shipments.LoadType);
        return hashCode;
    }
}
public class RoutesComparer : IEqualityComparer<Routes>
{
    public bool Equals(Routes route1, Routes route2) =>
        route1.DockCode == route2.DockCode && route1.CarrierArrival.Date == route2.CarrierArrival.Date &&
        route1.CarrierArrival.Hour == route2.CarrierArrival.Hour && route1.CarrierArrival.Minute == route2.CarrierArrival.Minute;


    public int GetHashCode(Routes obj) =>
    obj.DockCode.GetHashCode() ^ (obj.CarrierArrival.GetHashCode() * 13);
}

public class Routes
{
    public object DockCode { get; internal set; }
    public DateTime CarrierArrival { get; internal set; }
}
公共类路由比较程序:IEqualityComparer
{
公共布尔等于(路由路由1、路由2)=>
route1.DockCode==route2.DockCode&&route1.CarrierArrival.Date==route2.CarrierArrival.Date&&
route1.CarrierArrival.Hour==route2.CarrierArrival.Hour和route1.CarrierArrival.Minute==route2.CarrierArrival.Minute;
public int GetHashCode(路由对象)=>
obj.DockCode.GetHashCode()^(obj.CarrierArrival.GetHashCode()*13);
}
公务舱路线
{
公共对象DockCode{get;内部集合;}
public DateTime CarrierArrival{get;内部集合;}
}
过滤后的

    public ObservableCollection<Person> People1 { get; } = new ObservableCollection<Person>()
        {
            new Person("Donald", "Duck"),
            new Person("Daisy", "Duck"),
            new Person("Jack", "Daniels")
        };

    public ObservableCollection<Person> People2 { get; } = new ObservableCollection<Person>()
        {
            new Person("Donald", "Duck"),
            new Person("Daisy", "Duck"),
            new Person("Jim", "Beam")
        };

    public ObservableCollection<Person> PeopleInBothCollections { get; } = new ObservableCollection<Person>();

    public void RenderPeople()
    {
        List<Person> people = People1.Where(prs => People2.Contains(prs))
            .ToList();


        for (int i = PeopleInBothCollections.Count-1; i >=0 ; i--)
        {
            if (!people.Contains(PeopleInBothCollections[i]))
                PeopleInBothCollections.RemoveAt(i);
        }

        foreach (Person person in people)
        {
            if (!PeopleInBothCollections.Contains(person))
                PeopleInBothCollections.Add(person);
        }
    }
    public ObservableCollection<Routes> Loads { get; } = new ObservableCollection<Routes>();

    public ObservableCollection<Routes> Routes { get; } = new ObservableCollection<Routes>();

    public ObservableCollection<Shipments> ShipmentsColl { get; } = new ObservableCollection<Shipments>();

    public void RenderRoutes()
    {
        var hashRoutes = new HashSet<Routes>(new RoutesComparer());
        HashSet<Shipments> hashShipments = new HashSet<Shipments>(new ShipmentsComparer());
        foreach (var item in Loads)
        {
            if (item.ShipTo.Contains(" "))
            {
                foreach (var item2 in Routes.Where(d => d.DockCode == item.ShipTo.Substring(0, item.ShipTo.IndexOf(" ")) && d.CarrierDeparture.TimeOfDay == item.ShipTime.TimeOfDay))
                {
                    if (hashRoutes.Add(item2))
                    {
                        // Forming a collection of results
                        hashShipments.Add( new Shipments { Arrival = item2.CarrierArrival, Departure = item2.CarrierDeparture, Issuer = item.Customer, Destination = item.ShipTo, LoadType = item.LoadType });
                    }
                }
            }
        }


        // We bring in line the public collection of the resulting collection of results.
        for (int i = ShipmentsColl.Count-1; i >=0; i--)
        {
            if (hashShipments.Contains(ShipmentsColl[i]))
                hashShipments.Remove(ShipmentsColl[i]);
            else
                ShipmentsColl.RemoveAt(i);
        }

        // Add missing results.
        foreach (Shipments shipments in hashShipments)
        {
            ShipmentsColl.Add(shipments);
        }
    }
public observetecollection加载{get;}=new observetecollection();
公共ObservableCollection路由{get;}=new ObservableCollection();
public observetecollection ShipmentsColl{get;}=new observetecollection();
公共void RenderRoutes()
{
var hashRoutes=newhashset(newroutescomparer());
HashSet HashShippings=newHashSet(new ShipmentsComparer());
foreach(负载中的var项目)
{
如果(项目.ShipTo.Contains(“”))
{
foreach(Routes.Where(d=>d.DockCode==item.ShipTo.Substring(0,item.ShipTo.IndexOf(“”)和&d.CarrierDeparture.TimeOfDay==item.ShipTime.TimeOfDay))中的var item2)
{
if(hashRoutes.Add(第2项))
{
//形成一个结果集合
hashShippings.Add(新装运{到达=item2.CarrierArrival,离开=item2.CarrierDeparture,发卡机构=item.Customer,目的地=item.S