0

i have linq to sql query which i am using to bind grid

var query = (from bottom in
                                 (from d in context.tbl_pl_data
                                  where (d.lng_clientid.Equals(ClientId) && d.str_name.Equals(name) && d.int_deleted.Equals(0))
                                  orderby d.dte_mod_on descending
                                  select d).AsEnumerable()
                             select new tbl_pl_data
                                        {

                                            lng_id = bottom.lng_id,
                                            str_itemdsc = bottom.str_itemdsc,
                                            dte_cr_on = bottom.dte_cr_on,
                                            str_cr_by = bottom.str_cr_by,
                                            str_mod_by = bottom.str_mod_by,
                                            dte_mod_on = bottom.dte_mod_on
                                        }).ToList().OrderByDescending(d=>d.dte_mod_on).ToList();

Now i want to show this data according to year. I have session that has value of year.. value could be null or list type having single year or multiple year..i mean 2009 or 2009 and 2010. I am getting years value from below session.

Session["UserYearSelected"] = model.str_year_selected.Split(',').ToList();

Now how do i change my above query so that it accepts null or list of year value and compare with dte_cr_on column which is datetime and show data for that particular years only

4

2 回答 2

1

我将对此进行尝试并侦察您需要以下内容:

from d in context.tbl_pl_data
where (d.lng_clientid.Equals(ClientId) && d.str_name.Equals(name) && d.int_deleted.Equals(0)
      && d.dte_cr_on.Year.Contains(Session["UserYearSelected"])

但是,如果有多年,您可能需要转换Session["UserYearSelected"]string[]

显然无法测试您的查询或检查它是否编译,但希望它会对您有所帮助。

你也可以看看这个以获得进一步的帮助。

于 2013-08-16T13:07:54.217 回答
0

感谢恶魔的链接..它帮助我解决了问题..这是解决方案

string[] split=new string[]{};
            if (HttpContext.Current.Session["UserYearSelected"] != null)
            {
                split = HttpContext.Current.Session["UserYearSelected"].ToString().Split(',');
            }
var query = (from anode in
                             (from d in context.tbl_pl_data
                              where (d.lng_clientid.Equals(ClientId) && d.str_name.Equals(name) && d.int_deleted.Equals(0) && (split.Length==0 || split.Contains(d.dte_cr_on.Year.ToString())))
于 2013-08-17T03:54:57.370 回答