2

How do I merge two lists when using REST services? You can't use union with linq and dataservicequery so I'm not sure where to go.

The following doesn't work with dataservicequery...

var docQuery = (from docs in context.Documents.AsQueryable()
                        select new DocsColumns()
                        {
                            Title = docs.Title
                            //     Element = docs.Elements.Title 
                        })
                        .Union(from rules in context.Rules.AsQueryable()
                               select new DocsColumns()
                               {
                                   Title = rules.Title
                                   //    Element = rules.Elements.Title 
                               });

                               as DataServiceQuery<DocsColumns>;

This link doesn't help much either.... https://stackoverflow.com/questions/720609/merge-two-object-lists-with-linq

Any ideas? Really struggling on this one.

1

Using Linq, You can merge using below pseudo code :

using (DataContext context = new DataContext("<<Site Url>>"))
{

    EntityList<Item> allDocuments = context.GetList<Item>("Documents");
    EntityList<Item> allRules = context.GetList<Item>("Rules");

    List<Item> allDocumentItems = <<Select relevant docs from allDocuments>>;    
    List<Item> allRuleItems = <<Select relevant rules from allRules>>;

    IEnumerable<Item> mergedItems = allDocumentItems.Union(allRuleItems);
}

Note that Your source code must provide a declaration of an Item class, and that class must declare properties for each of the columns in the content type that your queries reference. For e.g.:

[ContentType(Name="Item", Id="0x01")]
public partial class Item
{
    [Column(Name = "Title", FieldType = "Text")] 
    public String Title { get; set; }
}
  • I'm using the client-side of things so Linq-to-Sharepoint won't work. – CeeMoney Nov 3 '11 at 22:26
  • 1
    Doesn't really matter. If you want to union stuff using Linq, you first need to retrieve everything you want to union in the first place. Where that data comes from doesn't matter to linq, at least not when you've already got the data in lists. So query both of your REST sources, map the data to lists of the same class type and union those. – Jasper Apr 1 '12 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.