使用LINQ创建ObservableCollection类

时间:2016-07-09 07:05:59

标签: c# wpf linq

我正在尝试使用LINQ查询创建一个单独的类,以用于绑定到WPF表单。我有下面的代码,我已经从这个网站和其他人的研究中放在一起,但得到一个编译错误说明。

  

无法隐式转换类型   ' System.Linq.IQueryable'至   system.Collections.ObjectModel.ObservableCollection&#39 ;.   存在显式转换(您是否错过了演员?)

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()但是遇到了这个编译错误,

  

无法隐式转换类型   &#39; System.Collections.Generic.List&#39;至   &#39; System.Collections.ObjectModel.ObservableCollection&#39;

我希望我能清楚自己要做什么以及对我做错了什么有任何帮助?

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

2 个答案:

答案 0 :(得分:2)

请尝试以下代码:

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()
                 );

请参阅this MSDN链接

答案 1 :(得分:-1)

这对我有用:

//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();
    }

}

我希望这对你有所帮助。