locked
Linq .FirstOrDefault() returning a single object error RRS feed

  • Question

  • User-250171173 posted

    Hello, hoping someone can let me know why I'm having trouble returning a single object...  thx

    Here is the linq code 

        public static IEnumerable<SingleStatusReport> GetStatusReport(int id)
        {                
            var status = (
                from w in db.Status
                where w.ProjectRef == id
                orderby w.CreatedDate descending
                select new SingleStatusReport { ID = w.ID, Description = w.Description, CreatedDate = w.CreatedDate }).FirstOrDefault();
    
            return status;
        }

    Here is the data model

    public class SingleStatusReport
        {
            public int ID { get; set; }
            public Nullable<System.DateTime> CreatedDate { get; set; }
            public string Description { get; set; }
        }

    Here is the error

    CS0029: Cannot implicitly convert type 'AnonymousType#1' to 'System.Collections.Generic.List<ProjectData.Models.SingleStatusReport>'

    Thursday, July 23, 2015 7:43 AM

Answers

  • User37182867 posted

    The problem is not in your Linq statement, its in your Function Signature.

    Try changing it to

    public static SingleStatusReport GetStatusReport(int id)

    which should hand your error of

    CS0029: Cannot implicitly convert type 'AnonymousType#1' to 'System.Collections.Generic.List<ProjectData.Models.SingleStatusReport>'

    However you will need to updated your variable type to be type specific as well... so make this change too

    var status = (
    change to
    SingleStatusReport status = (
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 9:23 AM

All replies

  • User467960235 posted

    Refer this link.

    http://stackoverflow.com/questions/17836799/cannot-implicitly-convert-type-listanonymoustype1-to-listwebapplication

    It may help you

    Thursday, July 23, 2015 8:05 AM
  • User-250171173 posted

    replacing FirstOrDefault() with ToList() the code works fine, however it returns multiple of records.

    I want to return just the most recent record hence the .FirstOrDefault().  It is the use of .FirstOrDefault() that is causing the error, and I don't understand why, or how I may fix it.  

    Thursday, July 23, 2015 8:15 AM
  • User37182867 posted

    The problem is not in your Linq statement, its in your Function Signature.

    Try changing it to

    public static SingleStatusReport GetStatusReport(int id)

    which should hand your error of

    CS0029: Cannot implicitly convert type 'AnonymousType#1' to 'System.Collections.Generic.List<ProjectData.Models.SingleStatusReport>'

    However you will need to updated your variable type to be type specific as well... so make this change too

    var status = (
    change to
    SingleStatusReport status = (
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 9:23 AM
  • User-775946742 posted

    You  are return different type of value in program

     public static IEnumerable<SingleStatusReport> GetStatusReport(int id)
        {                
            var status = (
                from w in db.Status
                where w.ProjectRef == id
                orderby w.CreatedDate descending
                select new SingleStatusReport { ID = w.ID, Description = w.Description, CreatedDate = w.CreatedDate }).FirstOrDefault();
    
            return status;
        }

    you can change this

     public static IEnumerable<SingleStatusReport>  GetStatusReport(int id) 
    {
    var status = ( from w in db.Status where w.ProjectRef == id orderby w.CreatedDate descending select
    new SingleStatusReport { ID = w.ID, Description = w.Description, CreatedDate = w.CreatedDate })
    .
    tolist();
    return status;
    }
     
    Friday, July 24, 2015 2:13 AM
  • User-271186128 posted

    Hi 2bitcoder,

    public static IEnumerable<SingleStatusReport> GetStatusReport(int id)
        {                
            var status = (
                from w in db.Status
                where w.ProjectRef == id
                orderby w.CreatedDate descending
                select new SingleStatusReport { ID = w.ID, Description = w.Description, CreatedDate = w.CreatedDate }).FirstOrDefault();
    
            return status;
        }

    The above method will return an IEnumerable<SingleStatusReport> value. If you are using the FistOrDefault() method in this method, it just return a SingleStatusReport.

    So, I suggest you could try to modify your code as below:

       public static SingleStatusReport GetStatusReport(int id)
        {                
            var status = (
                from w in db.Status
                where w.ProjectRef == id
                orderby w.CreatedDate descending
                select new SingleStatusReport { ID = w.ID, Description = w.Description, CreatedDate = w.CreatedDate }).FirstOrDefault();
    
            return status;
        }

    Or, you could also try to use the following code:

        public static IEnumerable<SingleStatusReport> GetStatusReport(int id)
        {                
            var status = (
                from w in db.Status
                where w.ProjectRef == id
                orderby w.CreatedDate descending
                select new SingleStatusReport { ID = w.ID, Description = w.Description, CreatedDate = w.CreatedDate }).ToList();
    
            return status;
        }

    Then, use the following code to get the first SingleStatusReport.

       var query2 = GetStatusReport(id).FirstOrDefault();

    Best Regard,
    Dillion

    Friday, July 24, 2015 4:12 AM