locked
Is this possible? 2 seperates queries in 1... RRS feed

  • Question

  • Hi!

    I have the following queries: 

     

    var userItems = this.ObjectContext.UserItems
    	.Include("BaseItem.Items.Language")
    	.Include("BaseItem.Level")
    	.Where(ui => ui.UserId == userId)
    	.ToList();
    
    List<IOrderedEnumerable<Item>> items = new List<IOrderedEnumerable<Item>>();
    foreach (var ui in userItems)
    {
    	var tempItems = ui.BaseItem.Items.Where(p => p.Language.TwoLetterIso.ToLower() == "en")
    		.GroupBy(p => p.BaseItem)
    		.Select(o => o.OrderBy(i => i.Language.SortNb));
    	items.AddRange(tempItems);
    }
    
    return items;
    

     


    I want to know if its not possible to include the 2nd query in the first one....

    Thanks for help!

    Bastien



    Sunday, June 12, 2011 12:39 PM

Answers

  • I've tried some things and this seems to work:

     

    			var currentUserId = (Guid)Membership.GetUser().ProviderUserKey;
    
    			var query = this.ObjectContext.Items
    				.Include("Language")
    				.Include("BaseItem.Level")
    				.Where(p => p.Language.TwoLetterIso.ToLower() == "en")
    				.Where(p => p.BaseItem.UserItems.Any(ui => ui.UserId == currentUserId))
    				.GroupBy(p => p.BaseItem)
    				.Select(o => o.OrderBy(i => i.Language.SortNb))
    				.ToList();
    
    			return query;
    


    --> I added the 2nd Where clause.

    Is this correct?

     

    Sunday, June 12, 2011 2:07 PM

All replies

  • I've tried some things and this seems to work:

     

    			var currentUserId = (Guid)Membership.GetUser().ProviderUserKey;
    
    			var query = this.ObjectContext.Items
    				.Include("Language")
    				.Include("BaseItem.Level")
    				.Where(p => p.Language.TwoLetterIso.ToLower() == "en")
    				.Where(p => p.BaseItem.UserItems.Any(ui => ui.UserId == currentUserId))
    				.GroupBy(p => p.BaseItem)
    				.Select(o => o.OrderBy(i => i.Language.SortNb))
    				.ToList();
    
    			return query;
    


    --> I added the 2nd Where clause.

    Is this correct?

     

    Sunday, June 12, 2011 2:07 PM
  • On 6/12/2011 10:07 AM, bPlatypus wrote:
    > I've tried some things and this seems to work:
    >
    >             var  currentUserId = (Guid)Membership.GetUser().ProviderUserKey;
    >
    >             var  query =this.ObjectContext.Items
    >                 .Include("Language")
    >                 .Include("BaseItem.Level")
    >                 .Where(p =>  p.Language.TwoLetterIso.ToLower() =="en")
    >                 .Where(p =>  p.BaseItem.UserItems.Any(ui =>  ui.UserId == currentUserId))
    >                 .GroupBy(p =>  p.BaseItem)
    >                 .Select(o =>  o.OrderBy(i =>  i.Language.SortNb))
    >                 .ToList();
    >
    >             return  query;
    >
    >
    > --> I added the 2nd Where clause.
    >
    > Is this correct?
    >
     
    Did it work? There is nothing stopping you from using a query within a
    query.
     
    Sunday, June 12, 2011 5:06 PM
  • Any update? Would you mind letting us know how it goes? Was your question solved?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 15, 2011 8:06 AM