Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Deleting a list of objects

Deleting a list of objects

Scheduled Pinned Locked Moved LINQ
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheShihan
    wrote on last edited by
    #1

    Hi, I try to delete a list of objects (all reservations for a given user). I've got problems with iterating the list, here's my code so far:

        public void DeleteReservationsByUserId(int userId)
        {
            using (AppEntities context = new AppEntities())
            {
                var reservations = from u in context.Users.Include("Reservations")
                                   where u.UserId == userId
                                   select u.Reservations;
    
                foreach (Reservation res in reservations.ToList())
                {
                    context.DeleteObject(res);
                }
    
                context.SaveChanges();
            }
        }
    

    The problem lies in the "foreach (Reservation res in reservations)" part.. it throws an exception, tell me that it cannot convert from one type to another.. but I don't understand, normally I can directly cast to eg. "IList" with ToList(). But normally I don't user a navigation property, so maybe they behave different than "normal" results? Any ideas? Shi

    A 1 Reply Last reply
    0
    • T TheShihan

      Hi, I try to delete a list of objects (all reservations for a given user). I've got problems with iterating the list, here's my code so far:

          public void DeleteReservationsByUserId(int userId)
          {
              using (AppEntities context = new AppEntities())
              {
                  var reservations = from u in context.Users.Include("Reservations")
                                     where u.UserId == userId
                                     select u.Reservations;
      
                  foreach (Reservation res in reservations.ToList())
                  {
                      context.DeleteObject(res);
                  }
      
                  context.SaveChanges();
              }
          }
      

      The problem lies in the "foreach (Reservation res in reservations)" part.. it throws an exception, tell me that it cannot convert from one type to another.. but I don't understand, normally I can directly cast to eg. "IList" with ToList(). But normally I don't user a navigation property, so maybe they behave different than "normal" results? Any ideas? Shi

      A Offline
      A Offline
      Adam Maras
      wrote on last edited by
      #2

      Here's your problem:

      var reservations = from u in context.Users.Include("Reservations")
      where u.UserId == userId
      select u.Reservations;

      The Reservations members in your User object is actually a collection of entities; what your select clause actually creates is an IQueryable<EntitySet<Reservation>> collection, not an IQueryable<Reservation> like you would expect. If you're trying to aggregate/concatenate the Reservations collections from multiple User objects, you need to look into using LINQ's Enumerable.SelectMany[^] extension method.

      T 1 Reply Last reply
      0
      • A Adam Maras

        Here's your problem:

        var reservations = from u in context.Users.Include("Reservations")
        where u.UserId == userId
        select u.Reservations;

        The Reservations members in your User object is actually a collection of entities; what your select clause actually creates is an IQueryable<EntitySet<Reservation>> collection, not an IQueryable<Reservation> like you would expect. If you're trying to aggregate/concatenate the Reservations collections from multiple User objects, you need to look into using LINQ's Enumerable.SelectMany[^] extension method.

        T Offline
        T Offline
        TheShihan
        wrote on last edited by
        #3

        Ok, that was it. Thank you! For more information see: http://weblogs.asp.net/zeeshanhirani/archive/2008/03/26/select-many-operator-part-1.aspx[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups