none
Querying OData feed with an anonymous type returned RRS feed

  • Question

  • I've bene using the DataServiceCollection to query my OData service so far. However I now need to create an anonymous type in my query (from a join where one part of the join is coming from a local collection of items) and return that collection. Is it possible to query a OData service when the LINQ expression returns an anymous type? I tried creating an class in my project that mirrors exactly the anonymous type's properties, but it isn't resolving...

    Is this even possible? I'm not tied ot the DataServiceCollectoin in this instance... I just want to issue the query and get results... any alternatives? Here's what I've got so far...

    DataServiceCollection<VideoInModule> _dataCollectionVideoInModule = new DataServiceCollection<VideoInModule>();
    
    public void GetModuleVideo(int moduleID, Action<IEnumerable<VideoInModule>> callback) {
     var query = from mv in ModuleVideoCollection
           where mv.ModuleID == moduleID
           select new {
            VideoID = mv.VideoID,
            OrderInModule = mv.ModuleVideoOrder
           };
     var query2 = from video in videos
            join moduleVideo in query
            on new { video.VideoID } equals new { moduleVideo.VideoID }
            select new {
             Video = video,
             OrderInModule = moduleVideo.OrderInModule
            };
    
     // execute query
     _dataCollectionVideoInModule.Clear();
     _dataCollectionVideoInModule.LoadCompleted += (sender, e) => {
      List<VideoInModule> list = new List<VideoInModule>();
      foreach (var item in _dataCollectionVideoInModule) {
       list.Add(new VideoInModule { Video=item.Video, OrderInModule=item.OrderInModule});
      }
      callback(list);
     };
     
     // this is the line that is tripping up as it doesn't like the query2 object being passed in... 
     // says no overload method to match
     _dataCollectionVideoInModule.LoadAsync(query2);
    }
      
    public class VideoInModule {
    
     public Video Video { get; set; }
     public int OrderInModule { get; set; }
    
    }
    
    
    

    -AC [MVP SharePoint Server] http://www.andrewconnell.com/blog
    Thursday, December 16, 2010 12:20 PM

Answers

  • Hi,

    LINQ should work just fine with anonymous types, the only catch is that the results will not be tracked by the context (if you modify properties on it, which you can't in C# anyway, you won't be able to mark it as updated in the context).

    In the above sample I think the problem is that you're trying to load IQueryable<anonymoustype> into a DataServiceCollection<VideoInModule>, that can't work.
    If you need to execute a query asynchronously, you should be able to cast the query2 to a DataServiceQuery type and call BeginExecute on it, and then once its done EndExecute. But it will be hard to process the results, since C# won't make it super easy for you. In this case I would suggest you use the predefined class instead of anonymous type, then you can cast the query to DataService<VideoInModule> and the EndExecute will return you a strongly typed enumeration.

    Thanks,


    Vitek Karas [MSFT]
    Thursday, December 16, 2010 2:14 PM
    Moderator

All replies

  • Hi,

    LINQ should work just fine with anonymous types, the only catch is that the results will not be tracked by the context (if you modify properties on it, which you can't in C# anyway, you won't be able to mark it as updated in the context).

    In the above sample I think the problem is that you're trying to load IQueryable<anonymoustype> into a DataServiceCollection<VideoInModule>, that can't work.
    If you need to execute a query asynchronously, you should be able to cast the query2 to a DataServiceQuery type and call BeginExecute on it, and then once its done EndExecute. But it will be hard to process the results, since C# won't make it super easy for you. In this case I would suggest you use the predefined class instead of anonymous type, then you can cast the query to DataService<VideoInModule> and the EndExecute will return you a strongly typed enumeration.

    Thanks,


    Vitek Karas [MSFT]
    Thursday, December 16, 2010 2:14 PM
    Moderator
  • The anonymous type is there to only select a subset of the entity.  Your suggestion means retrieving the whole entity, which sort of misses the point of the anonymous select.
    Thursday, October 18, 2018 10:28 AM