7

I have a LINQ statement that returns an anonymous type. I need to get this type to be an ObservableCollection in my Silverlight application. However, the closest I can get it to a

List myObjects;

Can someone tell me how to do this?

ObservableCollection<MyTasks> visibleTasks = e.Result;
var filteredResults = from visibleTask in visibleTasks
                      select visibleTask;

filteredResults = filteredResults.Where(p => p.DueDate == DateTime.Today);
visibleTasks = filteredResults.ToList();  // This throws a compile time error

How can I go from a anonymous type to an observable collection?

Thank you

DOK
  • 32,337
  • 7
  • 60
  • 92
user208662
  • 10,869
  • 26
  • 73
  • 86

5 Answers5

6

As Ekin suggests, you can write a generic method that turns any IEnumerable<T> into an ObservableCollection<T>. This has one significant advantage over creating a new instance of ObservableCollection using constructor - the C# compiler is able to infer the generic type parameter automatically when calling a method, so you don't need to write the type of the elements. This allows you to create a collection of anonymous types, which wouldn't be otherwise possible (e.g. when using a constructor).

One improvement over Ekin's version is to write the method as an extension method. Following the usual naming pattern (such as ToList or ToArray), we can call it ToObservableCollection:

static ObservableCollection<T> ToObservableCollection<T> 
  (this IEnumerable<T> en) { 
    return new ObservableCollection<T>(en); 
} 

Now you can create an observable collection containing anonymous types returned from a LINQ query like this:

var oc = 
  (from t in visibleTasks   
   where t.IsSomething == true
   select new { Name = t.TaskName, Whatever = t.Foo }
  ).ToObservableCollection();
BTownTKD
  • 7,911
  • 2
  • 31
  • 47
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • Is there a way to do it without using generics? I mean, with inline code and without creating a real type. Something like ObservableCollection ? – v1n1akabozo Jun 12 '16 at 16:20
2

Something like this would do the job using type inference features:

private static ObservableCollection<T> CreateObservable<T>(IEnumerable<T> enumerable)
{
    return new ObservableCollection<T>(enumerable);
}

static void Main(string[] args)
{

    var oc = CreateObservable(args.Where(s => s.Length == 5));
}
Ekin Koc
  • 2,996
  • 20
  • 24
1

You should just be able to do this:

visibleTasks = new ObservableCollection<MyTasks>(filteredResults);
BFree
  • 102,548
  • 21
  • 159
  • 201
0

Try:

var filteredResults = from visibleTask in visibleTasks
                      where(p => p.DueDate == DateTime.Today)
                      select visibleTask).ToList(); 

(filteredResults will contain your desired list)

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
ramnz
  • 631
  • 1
  • 6
  • 24
0

Are you sure that your object is an ObservableCollection indeed? If yes, you can just cast: visibleTasks = (ObservableCollection)filteredResults;

Vlad
  • 35,022
  • 6
  • 77
  • 199