0

I am trying to make a separate class with a LINQ query to use for binding to a WPF form. I have the code below that I have put together from the research on this site and others, but get a compile error stating.

Cannot implicitly convert type 'System.Linq.IQueryable' to system.Collections.ObjectModel.ObservableCollection'. An explicit conversion exists (are you missing a cast?)

Staff_Time_TBL is a class that was autogenerated when making a LINQ to SQL. Staff_Time_TBL is the name of a Table in SQL Server which what I am trying to get data from.

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

I tried to use Tolist() but get this compile error,

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.ObjectModel.ObservableCollection'

I hope I have be clear on what I am trying to do and any help as to What am I doing wrong?

Edit: This has more to do with implementing the Collection.

KyloRen
  • 2,691
  • 5
  • 29
  • 59
  • 5
    This question is not clear at all. It looks barely different from your other one, [LINQ query into ObservableCollection?](https://stackoverflow.com/questions/38277807/linq-query-into-observablecollection). What do you mean by "separate class"? In what way is `ObservableCollection` not doing what you want? In what way did the answer in your other question, which completely addressed the _compiler_ error you are asking about here, not accomplish what you want. – Peter Duniho Jul 09 '16 at 07:10
  • Why do you continue to try to assign things that are not `ObservableCollection` to variables that are, even though you already have an answer to your other question telling you how to correctly get an `ObservableCollection` object with the elements from your query in it? – Peter Duniho Jul 09 '16 at 07:10
  • @PeterDuniho, if I knew that I am sure I would not have posted the question. I don't know any other way to put it, is it a method inside a class what I am trying to do? – KyloRen Jul 09 '16 at 07:19
  • @PeterDuniho, José Caballero answer cleared up the issues I was having. – KyloRen Jul 09 '16 at 07:36
  • _"José Caballero answer cleared up the issues I was having"_ -- I don't know how. His answer is functionally no different at all from the answer you got to your other question. The end result is exactly the same. You could just as easily write `observableSomeClass = new ObservableCollection(result);`. The final state of the `observableSomeClass` object in each version of code would be indistinguishable from each other. – Peter Duniho Jul 09 '16 at 07:46
  • @PeterDuniho,please don't assume I know as much as you. If I knew what I was doing wrong, there would be no need for the question. It was a case of implementation, after seeing his answer I only now see how similar they are and that I was asking the same question in a different way.BTW, also try not to assume that everybody went through formal education when learning programming.I have no teacher, so no one to turn to. Can I ask you a question, from the time you started learning a programming language,did you have or was able to ask someone to physically explain a problem to you had it to you? – KyloRen Jul 09 '16 at 08:19
  • Don't assume that I'm assuming any of the things you are assuming I'm assuming. Part of learning is know how to ask good questions. There is a lot of guidance in that respect, including articles here on Stack Overflow such as [ask] and [mcve], and unfortunately your questions don't show the useful techniques for asking that guidance describes. It's great when you have great teachers available, but that's rare; it's critical a person who wants to learn, develop good habits that will elicit good, useful answers even from average people. – Peter Duniho Jul 09 '16 at 17:49

2 Answers2

2

Please try this code:

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

Please refer this MSDN link

S2S2
  • 8,322
  • 5
  • 37
  • 65
-1

This worked for me:

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

}

I hope this may help you.

José Caballero
  • 196
  • 1
  • 2
  • 10
  • Thank you very much, that clears up a lot of what I was having trouble with. I can finally see now why my other question is relevant. – KyloRen Jul 09 '16 at 07:35