日期:2014-05-20  浏览次数:20747 次

日期比较问题
在数据库中有日期型字段内容是 2010-1-12 0:00:00 这种格式

现程序中传入日期是 2010-1 这种格式,请问怎么比较二者相同。

------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] starrr = new string[] { "2010-1-12 0:00:00", "2010-1-22 0:00:00", "2010-2-12 0:00:00" };
            string str = "2010-1";
            var result = from s in starrr
                         where s.IndexOf(str) !=-1
                         select s;
            foreach(string s in result)
                Console.WriteLine(s);



        }
    }
}

------解决方案--------------------
上面少删除一点东西了。。。
C# code

var result = from t in dc.talbe
            Where t.xxxx.Year.ToString().Contains(yyyy.Substring(0,4)))
               && t.xxxx.Month.ToString().Contains(yyyy.Substring(5,2)));

or:

var result = from t in dc.talbe
            Where t.xxxx.Date.ToString()Substring(0,7).Contains(yyyy)));