使用 LINQ 创建 ObservableCollection 类



我正在尝试创建一个单独的类,其中包含一个 LINQ 查询,用于绑定到 WPF 表单。我有下面的代码,我从这个网站和其他网站上的研究中整理出来,但得到一个编译错误。

无法隐式转换类型 'System.Linq.IQueryable' to 系统。Collections.ObjectModel.ObservableCollection'. 存在显式转换(您是否缺少强制转换?

Staff_Time_TBL 是在制作 LINQ to SQL 时自动生成的类。 Staff_Time_TBL是SQL Server中Table的名称,我正在尝试从中获取数据。

class ObservableData
    {
        public ObservableCollection<Staff_Time_TBL> InfoData { get; set; }
        public ObservableData(DatabaseDataContext database, int staffNo, DateTime fromDate, DateTime toDate)
        {
            InfoData = new ObservableCollection<Staff_Time_TBL>();
            InfoData = database.Staff_Time_TBLs
                              .Where(staff => staff.Staff_No == staffNo &&
                                     staff.Date_Data == fromDate &&
                                     staff.Date_Data == toDate);
        }            
    }

我试图使用Tolist()但得到这个编译错误,

无法隐式转换类型 'System.Collections.Generic.List' to 'System.Collections.ObjectModel.ObservableCollection'

我希望我已经清楚我正在尝试做什么以及关于我做错了什么的任何帮助?

编辑:这与实现集合有关。

请尝试以下代码:

InfoData = new ObservableCollection<Staff_Time_TBL>
                 (
                   database.Staff_Time_TBLs
                   .Where(staff => staff.Staff_No == staffNo &&
                          staff.Date_Data == fromDate &&
                          staff.Date_Data == toDate).ToList()
                 );

请参阅此 MSDN 链接

这对我有用:

//Since i dont know what Staff_Time_TBL has or represents, i will use this as example
public class SomeClass
{
    public int number;
    public string text;
    public bool flag;
}
class Program
{
    static void Main(string[] args)
    {
        //Your observable collection
        ObservableCollection<SomeClass> observableSomeClass = new ObservableCollection<SomeClass>();
        //I am assuming that in your LINQ query you are using a list or a structure, this is not important at all
        List<SomeClass> listSomeClass = new List<SomeClass>();
        //Im just adding some data to my list to work with
        listSomeClass.Add(new SomeClass
        {
            number = 1,
            text = "ONE",
            flag =  true
        });
        listSomeClass.Add(new SomeClass
        {
            number = 2,
            text = "TWO",
            flag = false
        });
        listSomeClass.Add(new SomeClass
        {
            number = 3,
            text = "THREE",
            flag = true
        });
        //This is a example LINQ query which is going to return me the ones that flag is equals to true (2)
        var result = from node in listSomeClass
                     where node.flag == true
                     select new
                     {
                         node.number,
                         node.text,
                         node.flag
                     };
        //THIS IS THE TRICK:
        //Instead of trying making a convertion or a toList(), try iterating your result from your linq query and adding it to your observable structure
        foreach (var r in result)
        {
            observableSomeClass.Add(new SomeClass
            {
                number = r.number,
                text = r.text,
                flag = r.flag
            }
            );
        }
        Console.ReadLine();
    }

}

我希望这对你有所帮助。

相关内容

  • 没有找到相关文章

最新更新