C# IList<T> 到 ObservableCollection<T>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/731626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:59:55  来源:igfitidea点击:

IList<T> to ObservableCollection<T>

c#silverlight

提问by Steve Brouillard

I have a method in a Silverlight app that currently returns an IList and I would like to find the cleanest way to turn this into an ObservableCollection so:

我在 Silverlight 应用程序中有一个当前返回 IList 的方法,我想找到最简洁的方法将其转换为 ObservableCollection,因此:

public IList<SomeType> GetIlist()
{
   //Process some stuff and return an IList<SomeType>;
}

public void ConsumeIlist()
{
   //SomeCollection is defined in the class as an ObservableCollection

   //Option 1
   //Doesn't work - SomeCollection is NULL 
   SomeCollection = GetIlist() as ObservableCollection

   //Option 2
   //Works, but feels less clean than a variation of the above
   IList<SomeType> myList = GetIlist
   foreach (SomeType currentItem in myList)
   {
      SomeCollection.Add(currentEntry);
   }
}

ObservableCollection doesn't have a constructor that will take an IList or IEnumerable as a parameter, so I can't simple new one up. Is there an alternative that looks more like option 1 that I'm missing, or am I just being too nit-picky here and option 2 really is a reasonable option.

ObservableCollection 没有将 IList 或 IEnumerable 作为参数的构造函数,所以我不能简单地新建一个。是否有一个看起来更像我缺少的选项 1 的替代方案,或者我在这里太挑剔了,选项 2 确实是一个合理的选择。

Also, if option 2 is the only real option, is there a reason to use an IList over an IEnurerable if all I'm ever really going to do with it is iterate over the return value and add it to some other kind of collection?

另外,如果选项 2 是唯一真正的选项,如果我真正要做的只是遍历返回值并将其添加到其他类型的集合中,是否有理由在 IEnurerable 上使用 IList?

Thanks in advance

提前致谢

采纳答案by JaredPar

You could write a quick and dirty extension method to make it easy

您可以编写一个快速而肮脏的扩展方法以使其变得容易

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerable) {
  var col = new ObservableCollection<T>();
  foreach ( var cur in enumerable ) {
    col.Add(cur);
  }
  return col;
}

Now you can just write

现在你可以写

return GetIlist().ToObservableCollection();

回答by Randolpho

Er...

呃...

ObservableCollectiondoeshave a constructorthat will take an IEnumerable<T>, and IList<T>derives from IEnumerable<T>.

ObservableCollection确实有一个构造函数,它将采用IEnumerable<T>,并IList<T>IEnumerable<T>.

So you can "just new one up"

所以你可以“只是一个新的”

回答by Ragoczy

        IList<string> list = new List<string>();

        ObservableCollection<string> observable = 
            new ObservableCollection<string>(list.AsEnumerable<string>());

回答by Ireney Berezniak

The extension method that JaredPar has given you is your best option in Silverlight. It gives you the ability to turn any IEnumerable into observable collection automatically simply by refering to the namespace, and reduces code duplication. There is nothing built in, unlike WPF, which offers the constructor option.

JaredPar 为您提供的扩展方法是您在 Silverlight 中的最佳选择。它使您能够通过引用命名空间自动将任何 IEnumerable 转换为 observable 集合,并减少代码重复。与提供构造函数选项的 WPF 不同,没有内置任何内容。

ib.

伊布。

回答by bcleary

Not to reopen the thread but a constructor for ObservableCollection that takes IEnumerablehas been added to silverlight 4

不是重新打开线程,而是在 Silverlight 4 中添加了一个采用IEnumerable 的ObservableCollection 构造函数

回答by Simon_Weaver

Silverlight 4 DOES have the ability to just 'new up' an ObservableCollection

Silverlight 4 确实能够“更新”一个 ObservableCollection

Here's the shortened extension method possible in Silverlight 4.

这是 Silverlight 4 中可能的缩短的扩展方法。

public static class CollectionUtils
{
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> items)
    {
        return new ObservableCollection<T>(items);
    }
}

回答by water damage restoration

Dim taskList As ObservableCollection(Of v2_Customer) = New ObservableCollection(Of v2_Customer)
' Dim custID As Guid = (CType(V2_CustomerDataGrid.SelectedItem,  _
'         v2_Customer)).Cust_UUID
' Generate some task data and add it to the task list.
For index = 1 To 14
    taskList.Add(New v2_Customer() With _
                 {.Cust_UUID = custID, .Company_UUID, .City
                 })
Next

Dim taskListView As New PagedCollectionView(taskList)
Me.CustomerDataForm1.ItemsSource = taskListView

回答by Hunab Ku

You can do this:

你可以这样做:

public class SomeTypeCollection: ObservableCollection<SomeType>
{
    public SomeTypeCollection() : base() { }
    public SomeTypeCollection(IEnumerable<SomeType> IEObj) : base(IEObj) { 
 }
}

public ConsumeIlist
{
    public SomeTypeCollection OSomeTypeCllt { get; set; }
    MyDbContext _dbCntx = new MyDbContext();
     public ConsumeIlist(){
          OSomeTypeCllt = new 
   SomeTypeCollection(_dbCntx.ComeTypeSQLTable);
     }
}