从两个String项之间选择From List

时间:2015-03-17 15:50:24

标签: c# linq

我有这个列表,我的日期保存了字符串格式。例如,我如何在" 93/01 01"之间选择日期。和" 93/01/10"用LINQ?

code            date
 1            "93/01/01"
 2            "93/01/02"
 3            "93/01/03"
 4            "93/01/04"
 5            "93/01/05"
 6            "93/01/06"
 7            "93/01/07"
 8            "93/01/08"
 9            "93/01/09"
 10           "93/01/10"
 11           "93/01/11"
 12           "93/01/12"

2 个答案:

答案 0 :(得分:1)

对于将返回这些结果的查询, var asdf = liststuff.Where( x => x.date >= Convert.ToDateTime("1993/01/01") && x.date <= Convert.ToDateTime("1993/01/10"));

对于已执行并包含这些结果的列表, var asdf = liststuff.Where( x => x.date >= Convert.ToDateTime("1993/01/01") && x.date <= Convert.ToDateTime("1993/01/10")).ToList();

警示性评论都是正确的,但这样可以满足您的要求。

答案 1 :(得分:0)

public struct Data
    {
        public Data(string keyValue, string strValue)
        {
            key = keyValue;
            date =  Convert.ToDateTime(strValue);
        }

        public string key { get; private set; }
        public DateTime date { get; private set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var test = new List<Data>();
            test.Add(new Data("1", "93/01/01"));
            test.Add(new Data("2", "93/01/02"));
            test.Add(new Data("3", "93/01/03"));
            test.Add(new Data("4", "93/01/04"));
            test.Add(new Data("5", "93/01/05"));
            test.Add(new Data("6", "93/01/06"));
            test.Add(new Data("7", "93/01/07"));

            var asdf = test.Where(x => x.date >= Convert.ToDateTime("1993/01/01") && x.date <= Convert.ToDateTime("1993/01/10"));

        var asdff= from t in test
                   where t.date >= Convert.ToDateTime("1993/01/01") && t.date <= Convert.ToDateTime("1993/01/10")
                   select t;

        }
    }

只是为了像以前一样添加内容,导致尝试使用字符串&lt; = with date将无效。