Linq查询按月筛选 [英] Linq query filter by month

查看:450
本文介绍了Linq查询按月筛选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共课内容

{

public int Id {get;组; }

公共字符串名称{get;组; }

public string Date {get;组; }

}





Id名称日期

1 content1 01/01 / 2013

2 content2 05/01/2013



列表< content>每日= db.content.Where(u => Convert.ToDateTime(u.Date).Month == M_no))。ToList();



我' m尝试如果查询通过M_no'05',查询应该返回内容id 2。有没有人知道如何查询上述情况?

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 ?

推荐答案

你好,

试试这种方式

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


嗨Zaheer,



Hi Zaheer,

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





尝试在你的情况下采用这个。

这将你的月份从日期转换为整数你可以很容易与你的M-no比较。



祝你好运。



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.


试试这个样品



Try this sample

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();


这篇关于Linq查询按月筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆