如何在数据集中排序和选择数据?

时间:2017-06-20 06:24:37

标签: c# sorting dataset

我曾经对ObservableCollection对象的数据进行排序和选择,如:

public class MainViewModel : INotifyPropertyChanged
{

    private ObservableCollection<Module> myModulesList;
    private void RefreshModulesByOrder()
    {
        myModulesList = new ObservableCollection<Module>(sdb.GetOrderModules().OrderBy(mod => mod.Address));
        ModulesView = CollectionViewSource.GetDefaultView(myModulesList);
        ModulesView.Filter = obj =>
        {
            var Module = (Module)obj;
            return SelectedProduct != null && SelectedProduct.ModelNumber == Module.ModelNumber;
        };
    }

    private DataSet _ds;
    private void RefreshModules()
    {
        _ds = sdb.GetModules();
        _ds.Tables["Modules"].DefaultView.Sort = "Address";
        ModulesView = new ListCollectionView(_ds.Tables["Modules"].DefaultView)
        {
            Filter = obj =>
            {
                var Module = obj as DataRowView;
                return SelectedProduct != null && SelectedProduct.ModelNumber == Module["ModelNumber"].ToString();
            }
        };
    }

    public ushort[] DatagridToArray()
    {
        return _ds.Tables["Modules"].AsEnumerable().Select(mod => mod.Field<UInt16>("ParamValue")).ToArray();
    }

    private Product selectedProduct;
    public Product SelectedProduct
    { 

        get { return selectedProduct; }
        set
        {
            if (selectedProduct != value)
            {
                selectedProduct = value;
                NotifyPropertyChanged();
                RefreshProductList();
                RefreshModules();
                RefreshCommunication();
            }
        }
    }

}

2 个答案:

答案 0 :(得分:1)

代码包含大量错误。摆脱了所有编译器错误,但有很多代码丢失。请参阅:Implementing INotifyPropertyChanged - does a better way exist?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Windows.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    public class MainViewModel :  INotifyPropertyChanged
    {

        public static Product selectedProduct;
        static DataSet _ds = null;
        static Module sdb = new Module();
        ObservableCollection<Module> myModulesList { get; set; }
        static ICollectionView ModulesView = null;

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            //see : https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist

        }
        private static void NotifyPropertyChanged(Product product)
        {
        }
        private void RefreshModulesByOrder()
        {

            myModulesList = new ObservableCollection<Module>(sdb.GetOrderModules().OrderBy(mod => mod.Address));
            ModulesView = CollectionViewSource.GetDefaultView(myModulesList);
            ModulesView.Filter = obj =>
            {
                var Module = (Module)obj;
                return selectedProduct != null && selectedProduct.ModelNumber == Module.ModelNumber;
            };
        }

        private static Product RefreshModules()
        {
            _ds = sdb.GetModules();
            _ds.Tables["Modules"].DefaultView.Sort = "Address";
            ModulesView = new ListCollectionView(_ds.Tables["Modules"].DefaultView)
            {
                Filter = obj =>
                {
                    var Module = obj as DataRowView;
                    return selectedProduct != null && selectedProduct.ModelNumber == (int)Module["ModelNumber"];
                }
            };
            return null;
        }

        public ushort[] DatagridToArray()
        {
            return _ds.Tables["Modules"].AsEnumerable().Select(mod => mod.Field<UInt16>("ParamValue")).ToArray();
        }
        public static void RefreshProductList()
        {
        }
        public static void RefreshCommunication()
        {
        }
        public class Product
        {
            public int ModelNumber { get; set; }
            public Product SelectedProduct
            {

                get { return selectedProduct; }
                set
                {
                    if (selectedProduct != value)
                    {
                        selectedProduct = value;
                        NotifyPropertyChanged(this);
                        RefreshProductList();
                        RefreshModules();
                        RefreshCommunication();
                    }
                }
            }
        }
        public class Module
        {
            public int ModelNumber { get; set; }
            public int Address { get; set; }
            public DataSet GetModules()
            {
                return new DataSet();
            }
            public List<Module> GetOrderModules()
            {
                return new List<Module>();
            }
        }
    }
}

答案 1 :(得分:0)

这有效:

registerArray = _ds.Tables["Modules"].AsEnumerable().OrderBy(mod => mod.Field<UInt32>("Address")).Select(mod => mod.Field<UInt16>("ParamValue")).ToArray();