Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public class content
{
public int Id { get; set; }
public string name { get; set; }
public string Date { get; set; }
}


Id name Date
1 content1 01/01/2013
2 content2 05/01/2013

List<content> Daily = db.content.Where(u =>Convert.ToDateTime(u.Date).Month==M_no)).ToList();

I'm trying that if query passes M_no '05', query should return content id 2 . Is there anyone knows how to query above situation ?
Posted
Comments
Animesh Datta 30-May-14 2:24am    
what is the problem ?
Zaheer A.M. 30-May-14 2:40am    
its showing error
becoz Date is string datatype and im passing integer(M_no) value to compare.
Zaheer A.M. 30-May-14 2:45am    
List<content> Daily = db.content.Where(u =>Convert.ToDateTime(u.Date).Month.Equal(M_no)).ToList();
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression.
Animesh Datta 30-May-14 2:49am    
what is your datetime format ?
Zaheer A.M. 30-May-14 2:52am    
05/02/2014("MM/dd/yyyy")

Hello ,
try this way
List <content>= db.content.ToList().Where(u =>Convert.ToDateTime(u.Date).Month==M_no)).ToList(); //first convert to List then make the where conditon
</content>

thanks
 
Share this answer
 
Comments
Zaheer A.M. 30-May-14 3:06am    
Great Bro Its Works!!
thanks....
Animesh Datta 30-May-14 3:15am    
you are welcome . please accept the answer .
Ajith K Gatty 30-May-14 3:26am    
Good work bro!!
Animesh Datta 30-May-14 3:32am    
thanks Ajith
Hi Zaheer,

DateTime dt = DateTime.Now;
int month = Convert.ToInt32(dt.Month); 


try this to adopt in your case.
this converts your month from date to integer and u can easily compare with your M-no.

Good Luck.
 
Share this answer
 
v2
Comments
Zaheer A.M. 30-May-14 2:57am    
No bro it's not work
Zaheer A.M. 30-May-14 3:07am    
Thanks
Try this sample

C#
List<content> lst = new List<content>();
      lst.Add(new content() { Date = "01/01/2013", Id = 1, name = "content1 " });
      lst.Add(new content() { Date = "05/01/2013", Id = 2, name = "content2 " });

    var dfd =   lst.Where(k => DateTime.ParseExact(k.Date, "dd/MM/yyyy", null).Month == 1).ToList();
 
Share this answer
 
Comments
Zaheer A.M. 30-May-14 3:06am    
Thanks
C#
static void Eg2(int month)
       {
           List<content> obj = new List<content>(){
                new content(){ Id=1, name= "Prasad", Date="05/01/2013"} ,
                new content(){ Id=2, name= "Ram", Date="01/01/2013"},
                new content(){ Id=3, name= "Jay", Date="04/01/2013"},
                new content(){ Id=4, name= "Shekar", Date="05/06/2013"}
           };

           var result = obj.Where(r => Convert.ToDateTime(r.Date).Month == month);

           foreach (var item in result)
           {
               Console.WriteLine(item.Id + "\t" + item.name + "\t" + item.Date);
           }
       }
 
Share this answer
 

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