0
    public IEnumerable<Location> Locations
    {
        get { return (IEnumerable<Location>)GetValue(LocationsProperty); }
        set { SetValue(LocationsProperty, value); }
    }

    public static readonly DependencyProperty LocationsProperty =
        DependencyProperty.Register("Locations", typeof(IEnumerable<Location>), typeof(userControl), new PropertyMetadata(null));  




  public class Location
    {
        public db.Location DbLocation { get; set; }
        public ObservableCollection<db.Person> DbContactsInLocation { get; set; }
    }

    Locations = myDataContext.Locations
       .Where(l => l.Active == "Y")
       .OrderBy(l => l.Name)
       .Select(l => new Location { 
               DbLocation = l
               **DbContactsInLocation = l.PersonLocations.Select(pl => pl.personLocation.person)**
                }).ToList();

我越来越:

无法将类型 System.Collections.Generic.IEnumerable 隐式转换<myDataContext.Person>'为 'System.Collections.ObjectModel.ObservableCollection<myDataContext.Person>' 存在显式转换(您是否缺少演员表?)

有任何想法吗?

4

2 回答 2

1

您需要将 LINQ 查询插入到ObservableCollection<T>(IEnumerable<T>)构造函数中。试试这个:

DBContactsInLocation = new ObservableCollection<myDataContext.Person>(l.PersonLocations.Select(pl => pl.personLocation.person).ToList());
于 2013-11-05T20:21:27.830 回答
1

将您的 linq 查询包装在ObservableCollection<T>(IEnumerabele<T>)构造函数中。

DbContactsInLocation = new ObservableCollection<db.Person>(
  l.PersonLocations.Select(pl => pl.personLocation.person)
)
于 2013-11-05T20:21:30.417 回答