Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to call the following linq query to display latest date value but currently i keep experiencing compiling error:
API_09.Controllers.DateStampController.GetDate()': not all code paths return a value

C#
public IEnumerable<database_BD> GetDate()
        {

            var data = (from c in db.database_BD
                        select new
                        {
                            c.UploadDate

                        }).OrderByDescending(c => c.UploadDate).Take(1).ToList();

           //return data;

        }


if add in the return line, it shows another compiling error.
Please help. Is my approach to query is incorrect?

Thanks in advance.
Posted
Comments
Sanket Saxena 14-Apr-14 5:45am    
whats the error when you return the value coz it must.
miss786 14-Apr-14 5:50am    
Thanks for your response. when I add the 'return data' i get:

Cannot implicitly convert type 'System.Collections.Generic.List<anonymoustype#1>' to 'System.Collections.Generic.IEnumerable<######>'. An explicit conversion exists (are you missing a cast?) -- error.

I think you are not using the select new properly also need to use ToList() then apply OrderBy

Try below:

C#
public IEnumerable<database_bd> GetDate()
        {
 
            var data = (from c in db.database_BD
                        select new 
                        {
                            UploadDate=c.UploadDate
 
                        }).ToList().OrderByDescending(c => c.UploadDate).Take(1);
 
           return data;
 
        }
 
Share this answer
 
v3
Comments
miss786 14-Apr-14 6:11am    
hi, I am really sorry for to inform, i already tried using toList(), as above and it gives the same old error:

Cannot implicitly convert type 'System.Collections.Generic.List<anonymoustype#1>' to 'System.Collections.Generic.IEnumerable<######>'. An explicit conversion exists (are you missing a cast?) -- error.
Sanket Saxena 14-Apr-14 6:18am    
Then the problem is in var data. Could you remove it and take a IEnumerable/List Type
please try this one....
public IEnumerable<database_BD> GetDate()
        {
 
            var data = (from c in db.database_BD
                        select new database_BD()
                        {
                            c.UploadDate
 
                        }).OrderByDescending(c => c.UploadDate).Take(1).ToList();
 
           return data;

        }
 
Share this answer
 
Comments
miss786 14-Apr-14 5:54am    
Many thanks for your solution, but I am sorry to inform, I am getting another error -- Cannot initialize type 'API_09.database_BD' with a collection initializer because it does not implement 'System.Collections.IEnumerable'.
abhishek_singh 14-Apr-14 6:17am    
what is the type of database_BD, It must be entity of Updatedate type, make sure about that, because if this is another type entity then it can not convert to that format.
abhishek_singh 14-Apr-14 6:23am    
please go through below solution 3
please try complete code

public IEnumerable<database_BD> GetDate()
        {
 
            IEnumerable<database_BD> data = (from c in db.database_BD
                        select new database_BD()
                        {
                            UploadDate = c.UploadDate
 
                        }).OrderByDescending(c => c.UploadDate).Take(1).ToList();
 
           return data;
 
        }


C#
public class database_BD
       {
           public datetime UploadDate{ get; set; }           
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900