LINQ - Cast Error in this Linq Query - Asked By Donnie Talley on 06-Nov-11 04:45 AM

   /// <summary>
        /// Retrieves all of the items that a member currently has out on loan.
        /// </summary>
        /// <param name="memberId">The ID of the member for whose loans we are looking</param>
        /// <returns>A List containing  all Items the Member has on loan.</returns>
        public List<Item> GetItems(short memberId)
        {
            List<Item> list = null;
            Require.IsGreaterThan<int>(memberId, 0, "memberId");


            try
            {
                using ( LibraryDataContext dataContext = new LibraryDataContext() )
                {
                    list = from ql in dataContext.loans
                           let copy = ql.copy
                           let item = ql.copy.item
                           let title = ql.title
                           where ql.member_no == memberId
                           select new 
                           {
                               ql.isbn,
                               ql.copy_no,
                               ql.member_no,
                               ql.out_date,
                               ql.due_date,
                               ql.copy.on_loan,
                               ql.copy.item.translation,
                               ql.copy.item.cover,
                               ql.copy.item.loanable,
                               ql.title.title1,
                               ql.title.author,
                               ql.title.synopsis,
                           };


                    if ( list == null )
                    {
                        throw new LibraryException(ErrorCode.ItemNotFound);
                    }


                    List<Item> result = (List<Item>)list.ToList<Item>();


                    return result;
                }
                
            }
            catch (System.Exception ex)
            {
                throw new LibraryException(ErrorCode.GenericException);
            }


            
        
        }


This the error i keep when I compile this code what am I doing wrong 


Error 1 Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.List<DataTransferObjects.Item>'. An explicit conversion exists (are you missing a cast?)








Suchit shah replied to Donnie Talley on 06-Nov-11 06:27 AM
To convert IQuerable or IEnumerable to a list, you can do one of the following:

IQueryable<object> q = ...;
List<object> l = q.ToList();
or:

IQueryable<object> q = ...;
List<object> l = new List<object>(q);



IEnumerable<string> e = (from char c in source
                        select new { Data = c.ToString() }).Select(t = > t.Date);

then you can call ToList():

List<string> l = (from char c in source
                  select new { Data = c.ToString() }).Select(t = > t.Date).ToList();
Peter Bromberg replied to Donnie Talley on 06-Nov-11 11:34 AM
You didn't specify, but I assume the exception is being thrown on this line:

    List<Item> result = (List<Item>)list.ToList<Item>();

This is because you used an anonymous type in your query:
select new 
                           {
                               ql.isbn,
                               ql.copy_no,
                               ql.member_no, ....

However, you should still be able to convert this to List<DataTransferObjects.Item> 

Donnie Talley replied to Peter Bromberg on 06-Nov-11 03:08 PM
i figure out the answer to my question 
 /// <summary>
        /// Retrieves all of the items that a member currently has out on loan.
        /// </summary>
        /// <param name="memberId">The ID of the member for whose loans we are looking</param>
        /// <returns>A List containing  all Items the Member has on loan.</returns>
        public List<Item> GetItems(short memberId)
        {
            List<Item> list;
            Require.IsGreaterThan<int>(memberId, 0, "memberId");
            try
            {
                using ( LibraryDataContext dataContext = new LibraryDataContext() )
                {
                    list = (from ql in dataContext.loans
                           let copy = ql.copy
                           let item = ql.copy.item
                           let title = ql.title
                           where ql.member_no == memberId
                           select new Item
                           {
                               Isbn = ql.isbn,
                               CopyNumber = ql.copy_no,
                               MemberId = ql.member_no,
                               CheckoutDate = ql.out_date,
                               DueDate = ql.due_date,
                               Translation = item.translation,
                               Cover = item.cover,
                               Loanable = item.loanable.HasValue,
                               Title = title.title1,
                               Author = title.author,
                               Synopsis = title.synopsis,
                           }).ToList<Item>();


                    if ( list == null )
                    {
                        throw new LibraryException(ErrorCode.ItemNotFound);
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw new LibraryException(ErrorCode.GenericException, ex.Message);


            }


            return list;
        }
    
Donnie Talley replied to Suchit shah on 06-Nov-11 03:08 PM
 /// <summary>
        /// Retrieves all of the items that a member currently has out on loan.
        /// </summary>
        /// <param name="memberId">The ID of the member for whose loans we are looking</param>
        /// <returns>A List containing  all Items the Member has on loan.</returns>
        public List<Item> GetItems(short memberId)
        {
            List<Item> list;
            Require.IsGreaterThan<int>(memberId, 0, "memberId");
            try
            {
                using ( LibraryDataContext dataContext = new LibraryDataContext() )
                {
                    list = (from ql in dataContext.loans
                           let copy = ql.copy
                           let item = ql.copy.item
                           let title = ql.title
                           where ql.member_no == memberId
                           select new Item
                           {
                               Isbn = ql.isbn,
                               CopyNumber = ql.copy_no,
                               MemberId = ql.member_no,
                               CheckoutDate = ql.out_date,
                               DueDate = ql.due_date,
                               Translation = item.translation,
                               Cover = item.cover,
                               Loanable = item.loanable.HasValue,
                               Title = title.title1,
                               Author = title.author,
                               Synopsis = title.synopsis,
                           }).ToList<Item>();


                    if ( list == null )
                    {
                        throw new LibraryException(ErrorCode.ItemNotFound);
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw new LibraryException(ErrorCode.GenericException, ex.Message);


            }


            return list;
        }
    
Peter Bromberg replied to Donnie Talley on 06-Nov-11 03:11 PM
Exactly. Now it is not an anonymous type but an actual Item object.
Donnie Talley replied to Peter Bromberg on 06-Nov-11 03:13 PM
yes it was simple wont I used intellsense