Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am currently getting a an compiling error on the date property in the where clause code below:

error:
C#
string' does not contain a definition for 'UploadDate' and no extension method 'UploadDate' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)


C#
public HttpResponseMessage Get(DateTime? date)
       {
           if (User.IsInRole("admin"))
          {
              //IEnumerable<data_qy> data_queries;

               DateTime yesterday = DateTime.Today.AddBusinessDays(-4);

               var query = (from c in db.data_qy
                           where c.UploadDate == yesterday &&
                                 c.Cover == "prest"
                           select (c.Name)).OrderByDescending(a => a.UploadDate).Distinct();

               var data = query.ToList();

               if (!data.Any())
               {
                  var message = string.Format("No data found");
                   return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);

              }

I am trying to query data, where the name property is only displayed distinctly with the where clause filters.

Any guide would be very much appreciated.
Thanks
Posted
Updated 8-May-14 0:59am
v4

C#
var query = (from c in db.data_qy
           where c.UploadDate == yesterday && c.Cover == "prest"
           select (c.Name)).OrderByDescending(a => a.UploadDate).Distinct();


Here, you are selecting c.Name and hence your a is no longer of type data_qy, it becomes a string.

Try this:
C#
var query = (from c in db.data_qy
           where c.UploadDate == yesterday && c.Cover == "prest"
           select (c)).OrderByDescending(a => a.UploadDate).Distinct();


i.e. instead of selecting c.Name, select c, it should work.

EDIT:

Also, I see that you need only distinct Names, so you can get them as:
C#
var query = (from c in db.data_qy
           where c.UploadDate == yesterday && c.Cover == "prest"
           select (c)).OrderByDescending(a => a.UploadDate)
           .Select(a => a.Name).Distinct();
 
Share this answer
 
v2
Comments
miss786 8-May-14 6:59am    
Thank you so much for clarifying this issue up. You have as been a great help. Thanks a million. =D. Have a great day!
Agent__007 8-May-14 6:59am    
You are most welcome. :)
Sampath Lokuge 8-May-14 7:01am    
+ 5 :)
Agent__007 9-May-14 1:08am    
Thank you! :)
Hence your 'UploadDate' property is null-able, you have to use c.UploadDate.Value.

C#
var query = (from c in db.data_qy
                          where c.UploadDate.Value == yesterday &&
                                c.Cover == "prest"
                          select (c)).OrderByDescending(a => a.UploadDate.Value).Distinct();
 
Share this answer
 
v5
Comments
miss786 8-May-14 5:12am    
Thank you for your quick response back. I am sorry to inform, I had already tried this approach and it still shows me the same 'string' error. Please advice, if possible.
Sampath Lokuge 8-May-14 5:18am    
What is this 'AddBusinessDays'? Is that extension method or what ? Can you put that code snippet also ?
miss786 8-May-14 5:23am    
Yes that is correct. I have updated my code with the requested Date class, which has 'AddBusinessDays' method. Thank you for your time and help.
miss786 8-May-14 6:00am    
Apology, if you misunderstood my last comment. I am still getting the string error. if you could explain, why I am getting this error, then I can try something from there. I appreciate all your help and time.
Sampath Lokuge 8-May-14 6:07am    
Aha.. Ok sure.try this way.I couldn't see any property like this 'Cover' ? You can try by removing '.OrderByDescending(a => a.UploadDate.Value).Distinct()' and try that.After that let me know ?

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