วิธีการแปลง IEnumerable เป็น ObservableCollection


คำตอบ:


209

ตามMSDN

var myObservableCollection = new ObservableCollection<YourType>(myIEnumerable);

นี่จะทำสำเนา IEnumerable ปัจจุบันที่ตื้นและเปลี่ยนเป็น ObservableCollection


นี่เร็วกว่าการใช้วน ForEach เพื่อแทรกรายการทีละรายการลงในคอลเลกชันหรือไม่?
Rexxo

4
@Rexxo มันจะเร็วขึ้นเล็กน้อยที่จะทำกับนวกรรมิก ภายในใช้foreachเพื่อคัดลอกรายการไปยังคอลเลกชันภายใน แต่ถ้าคุณทำ foreach และโทรAdd มันจะผ่านInsertItemซึ่งจะทำสิ่งพิเศษมากมายที่ไม่จำเป็นเมื่อเริ่มต้นเติมทำให้ช้าลงเล็กน้อย
Scott Chamberlain

Simples และคำตอบที่ดีที่สุด
peterincumbria

74
  1. หากคุณทำงานกับบุคคลทั่วไปIEnumerableคุณสามารถทำได้ด้วยวิธีนี้:

    public ObservableCollection<object> Convert(IEnumerable original)
    {
        return new ObservableCollection<object>(original.Cast<object>());
    }
  2. หากคุณทำงานกับสามัญIEnumerable<T>คุณสามารถทำได้ด้วยวิธีนี้:

    public ObservableCollection<T> Convert<T>(IEnumerable<T> original)
    {
        return new ObservableCollection<T>(original);
    }
  3. หากคุณทำงานกับบุคคลทั่วไปIEnumerableแต่รู้ประเภทขององค์ประกอบคุณสามารถทำได้ด้วยวิธีนี้:

    public ObservableCollection<T> Convert<T>(IEnumerable original)
    {
        return new ObservableCollection<T>(original.Cast<T>());
    }

29

เพื่อทำให้สิ่งต่าง ๆ เรียบง่ายยิ่งขึ้นคุณสามารถสร้างวิธีการขยายออกจากมัน

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

จากนั้นคุณสามารถเรียกใช้เมธอดบน IEnumerable ทุกอัน

var lst = new List<object>().ToObservableCollection();

3
ObservableCollection<decimal> distinctPkgIdList = new ObservableCollection<decimal>();
guPackgIds.Distinct().ToList().ForEach(i => distinctPkgIdList.Add(i));

// distinctPkgIdList - ObservableCollection
// guPackgIds.Distinct() - IEnumerable 

1

ฟังก์ชัน C # เพื่อแปลง IEnumerable เป็น ObservableCollection

private ObservableCollection<dynamic> IEnumeratorToObservableCollection(IEnumerable source)
    {

        ObservableCollection<dynamic> SourceCollection = new ObservableCollection<dynamic>();

        IEnumerator enumItem = source.GetEnumerator();
        var gType = source.GetType();
        string collectionFullName = gType.FullName;
        Type[] genericTypes = gType.GetGenericArguments();
        string className = genericTypes[0].Name;
        string classFullName = genericTypes[0].FullName;
        string assName = (classFullName.Split('.'))[0];

        // Get the type contained in the name string
        Type type = Type.GetType(classFullName, true);

        // create an instance of that type
        object instance = Activator.CreateInstance(type);
        List<PropertyInfo> oProperty = instance.GetType().GetProperties().ToList();
        while (enumItem.MoveNext())
        {

            Object instanceInner = Activator.CreateInstance(type);
            var x = enumItem.Current;

            foreach (var item in oProperty)
            {
                if (x.GetType().GetProperty(item.Name) != null)
                {
                    var propertyValue = x.GetType().GetProperty(item.Name).GetValue(x, null);
                    if (propertyValue != null)
                    {
                        PropertyInfo prop = type.GetProperty(item.Name);
                        prop.SetValue(instanceInner, propertyValue, null);
                    }
                }
            }

            SourceCollection.Add(instanceInner);
        }

        return SourceCollection;
    }
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.