这是将 List转换为最佳方式吗?从模型到 ObservableCollection在视图中?
在MVVM开发中,我不断地将List
从我的模型转换为ObservableCollection< ;T>
代表我的观点。
我在.NET中寻找一种简洁地执行此操作的方法,例如.ToList<>
或.ToArray<>
或< code>.ToDictionary<> 但找不到 ObservableCollection 的类似内容。
因此我创建了以下扩展方法ConvertToObservableCollection
。
是否有更好的方法将 List
转换为 ObservableCollection
,或者每个 MVVM 开发人员最终都会在某个时候编写此扩展方法?< /强>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Collections.ObjectModel;
namespace TestObser228342
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<string> names = new List<string> { "one", "two", "three" };
ObservableCollection<string> ocNames =
names.ConvertToObservableCollection<string>();
ocNames.ToList().ForEach(n => Console.WriteLine(n));
List<Customer> customers = new List<Customer>
{
new Customer { FirstName = "Jim", LastName = "Smith" },
new Customer { FirstName = "Jack", LastName = "Adams" },
new Customer { FirstName = "Collin", LastName = "Rollins" }
};
ObservableCollection<Customer> ocCustomers =
customers.ConvertToObservableCollection<Customer>();
ocCustomers.ToList().ForEach(c => Console.WriteLine(c));
}
}
public static class StringHelpers
{
public static ObservableCollection<T> ConvertToObservableCollection<T>
(this List<T> items)
{
ObservableCollection<T> oc = new ObservableCollection<T>();
foreach (var item in items)
{
oc.Add(item);
}
return oc;
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return FirstName + " " + LastName;
}
}
}
In MVVM development I am constantly converting List<T>
from my models to ObservableCollection<T>
for my views.
I looked around in .NET for a way to succinctly do this e.g. such as .ToList<>
or .ToArray<>
or .ToDictionary<>
but couldn't find anything similar for ObservableCollection.
Therefore I made the following extention method ConvertToObservableCollection<T>()
.
Is there a better way to convert List<T>
to ObservableCollection<T>
, or does every MVVM developer end up writing this extension method at some point?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Collections.ObjectModel;
namespace TestObser228342
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<string> names = new List<string> { "one", "two", "three" };
ObservableCollection<string> ocNames =
names.ConvertToObservableCollection<string>();
ocNames.ToList().ForEach(n => Console.WriteLine(n));
List<Customer> customers = new List<Customer>
{
new Customer { FirstName = "Jim", LastName = "Smith" },
new Customer { FirstName = "Jack", LastName = "Adams" },
new Customer { FirstName = "Collin", LastName = "Rollins" }
};
ObservableCollection<Customer> ocCustomers =
customers.ConvertToObservableCollection<Customer>();
ocCustomers.ToList().ForEach(c => Console.WriteLine(c));
}
}
public static class StringHelpers
{
public static ObservableCollection<T> ConvertToObservableCollection<T>
(this List<T> items)
{
ObservableCollection<T> oc = new ObservableCollection<T>();
foreach (var item in items)
{
oc.Add(item);
}
return oc;
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return FirstName + " " + LastName;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用
ObservableCollection
< 的适当构造函数/a>?Why don't you use the appropriate constructor of
ObservableCollection
?在 Silverlight 4 中也可以使用 ObservableCollection 构造函数声明变量。
Declaring a variable with ObservableCollection constructor works in Silverlight 4 too.
自动映射器?
http://automapper.codeplex.com/wikipage?title=Lists%20and% 20数组
AutoMapper ?
http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays
你的解决方案似乎很复杂...也许我的太简单了..这是我编写的一个扩展方法,用于将我的nettiers集合转换为可观察的集合...它在vb.net中...但你会明白要点。 ..
这是转换后的 C#,
我想你可以使用 lambda,但我不理解它们,所以我避免使用它们。
your solution seems way to complicated...maybe mine is too simple..this is an extension method I wrote to convert my nettiers collections to observable collections...its in vb.net... but you'll get the gist...
here is the converted C#
I suppose to you could use a lambda, but I dont understand them, so I avoid them.