Come filtrare i dati da DataServiceCollection?Nel servizio dati WCF
-
21-12-2019 - |
Domanda
Ho ricevuto dati in DataServiceCollection Equipbookings, ma da questo elenco Voglio filtrare i dati, come per data selezionata dal selezionatore dei dati
Per questo cerco di scrivere questo, ma non funziona:
Ho ricevuto errori "Cannnot IMPLICITY Convert Elenco di tipo per DataServiceCollection
private DateTime _seletedDateChanged;
public DateTime SeletedDateChanged
{
get { return _seletedDateChanged; }
private set
{
_seletedDateChanged = value;
// here i filter collections
EquipBookings = FilterJobs(_seletedDateChanged);
NotifyPropertyChanged("SeletedDateChanged");
}
}
public DataServiceCollection<EquipBooking> FilterJobs(DateTime SeletedDateChanged)
{
return EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged).ToList();
}
.
Il codice completo è:
#region EquipBookings
// Define the binding collection for EquipBookings.
private DataServiceCollection<EquipBooking> _equipBookings;
public DataServiceCollection<EquipBooking> EquipBookings
{
get { return _equipBookings; }
private set
{
_equipBookings = value;
_equipBookings.LoadCompleted += OnEquipBookingLoaded;
NotifyPropertyChanged("EquipBookings");
}
}
public void LoadEquipBookingsData()
{
_context = new THA001_devEntities(_rootUri);
EquipBookings = new DataServiceCollection<EquipBooking>(_context);
var query = _context.EquipBooking.Expand("Status").Where(x => x.Status.Description.ToLower() == "confirmed").OrderBy(d => d.BookedFromDteTme);
EquipBookings.LoadAsync(query);
IsDataLoaded = true;
}
private void OnEquipBookingLoaded(object sender, LoadCompletedEventArgs e)
{
if (e.Error == null)
{
IsDataLoaded = false;
if (EquipBookings.Continuation != null)
{
EquipBookings.LoadNextPartialSetAsync();
EquipBookingList = EquipBookings;
}
}
}
. Soluzione
Questo restituisce un elenco:
return EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged).ToList();
.
Mentre il tipo di ritorno del metodo FilterJobs
è DataServiceCollection<EquipBooking>
e non vi è alcuna conversione implicita tra i due.Il compilatore non capisce come convertire l'uno sull'altro.
Potresti fare qualcosa del genere:
public DataServiceCollection<EquipBooking> FilterJobs(DateTime SeletedDateChanged)
{
var equipBookings = EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged);
var dataServiceCollection = new DataServiceCollection<EquipBooking>(equipBookings);
return dataServiceCollection;
}
.
C'è un sovraccarico del costruttore di DataServiceCollection
che prende un IEnumerable{T}
(un IEnumerable
di EquipBookings
nel tuo caso) come parametro.Convenientemente questo è esattamente ciò che il EquipBookings.Where(c => c.CreatedOn == SeletedDateChanged);
ritorna.
Altri suggerimenti
È necessario convertire generico IEnumerable in ObservableCollection.Puoi scrivere un metodo di estensione per fare questa conversione.Gli esempi sono riportati anche qui Come convertire IEnumerable in ObservableCollection?