简体   繁体   中英

how can we assign an Observable collection through EF LINQ statements

Is it possible to assign a collection from a database entity to an ObservableCollection using entity framework?

This is what iv tried so far I tried converting the list to an ObsrvableCollection using a constructor..but LINQ does not allow constructors which accept parameter.

Creating an extension for Ienumerable called ToObservable collection..but EF Does not recognize this.

Is there any other elegant solution to this. Given below is the code i am using...and fails

using (var db = new clientEntities())
{
                var data = from p in db.CLIENTs
                           select new Contracts.Client()
                                      {
                                          ClientID = p.CLIENT_ID,
                                          FirstName = p.FIRST_NAME,                                          
                                          PayInfo = new ObservableCollection<PayInfo>(p.PAY_INFO.Select(n=> new PaymentInfo(){ 
                                              PayID=n.ID                                                    
                                              }))
                                      };
    }

The PayInfofield is an Observable collection. Is it possible to elegantly assign it without using temporary variables.

It sounds like you're confusing LINQ to Entities/SQL with plain-old LINQ. Remember that when you're using the EF, any LINQ queries you write have to be translated into SQL. Thus, whatever object transformations you need to do on the data returned from the SQL database need to be directly supported by the EF. This includes ToList(), ToArray(), etc, but not your own custom extensions.

I think the best you can do here is first query the database (you could use ToList() so the query executes), then use that resulting list in the constructor of the ObservableCollection (or call your ToObservableCollection() extension method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM