c# - 将 IEnumerable 转换为 ObservableCollection
问题描述
我有这个 IEnumerable
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 };
}
}
}
}
}
}
我的目标是添加
yield return new Shipments { Arrival = item2.CarrierArrival, Departure = item2.CarrierDeparture, Issuer = item.Customer, Destination = item.ShipTo, LoadType = item.LoadType };
这变成了一个ObservableCollection
目前我正在使用这个 IEnumerable 来填充DataGrid
我无法继续以这种方式使用它,因为当数据网格刷新时它会删除用户选择的排序。
编辑:
类货件
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);
}
路由从 PDF 文件添加到ObservableCollection
负载从 SQL 语句添加到它们自己的ObservableCollection
public static ObservableCollection<Routes> Routes { get; set; } = new ObservableCollection<Routes>();
public static ObservableCollection<Loads> Loads { get; set; } = new ObservableCollection<Loads>();
目前我使用IEnumerable
上述
我希望它是一个ObservableCollection
而不是一个IEnumerable
以便于刷新DataGrid
并保持过滤的项目。
解决方案
我查看了您对主题问题所做的编辑。但是我仍然不了解问题的许多细节,因此我无法提供完全适合答案的实现:
加载元素是什么类型?
什么是对现有 Shipments 的更改,什么是新的 Shipments 实例?
Shipments 集合属性的非常不幸的名称与此集合的项目类型名称相同。
这是一个选项,据我了解您的任务。但我不能保证它会完全适合你。
ShipmentsComparer
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; }
}
过滤
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);
}
}
推荐阅读
- ios - UIScrollView bar 插入标签栏和导航栏的大小
- javascript - 未捕获的 ReferenceError:随机数未在 test.js 中定义
- php - 如何从旧数组中按每个重复值的计数降序创建新数组
- python - 将整数和字符串拆分为其他列表
- javascript - JS onclick,覆盖之前的点击
- python - 正则表达式 Python:保留第一个数字
- json - Chartist.js - 数据点不可见,格式化 json 数据存在问题
- go - 需要测试一个时间是否在同一天的另外两个时间之间
- arrays - What is the mathematical explanation of adding 2d Array and 1d Array?
- python - 一次将多个数据帧合并在一起