如何对此查询使用(动态)null概念

时间:2014-05-02 13:47:19

标签: linq asp.net-3.5

我想用不同类型的条件绑定网格视图这里显示我使用(动态)null概念进行查询声明但是在休息结束时我无法获得与查询相关联的字段。

这里我把我的代码:

 public void FillGrid(string GroupByText, string ColumnName, string SearchText)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var query = (dynamic)null;
            switch (GroupByText)
            {
                case "Enquiry":
                    query = db.Enquiries.Where(i => i.enttype_id.Equals("1")).OrderByDescending(i => i.Created_date).Select(i => new
                    {
                        Ref_no = i.Ref_no,
                        Name = db.Parties.Where(p => p.Id.Equals(i.party_id)).Select(p => p.Name).SingleOrDefault(),
                        StatusName = db.Status.Where(s => s.Id.Equals(i.status_id)).Select(s => s.StatusName).SingleOrDefault(),
                        CategoryName = db.Categories.Where(c => c.Id.Equals(i.category_id)).Select(c => c.category_name).SingleOrDefault(),
                        IsUregent = i.IsUregent,
                        Created_date = i.Created_date
                    }).FilterForColumn(ColumnName,SearchText).ToList();
                    break;
                case "Visit":
                    query = db.Enquiries.Where(i => i.enttype_id.Equals("2")).OrderByDescending(i => i.Created_date).Select(i => new
                     {
                         Ref_no = i.Ref_no,
                         Name = db.Parties.Where(p => p.Id.Equals(i.party_id)).Select(p => p.Name).SingleOrDefault(),
                         StatusName = db.Status.Where(s => s.Id.Equals(i.status_id)).Select(s => s.StatusName).SingleOrDefault(),
                         CategoryName = db.Categories.Where(c => c.Id.Equals(i.category_id)).Select(c => c.category_name).SingleOrDefault(),
                         IsUregent = i.IsUregent,
                         Created_date = i.Created_date
                     }).FilterForColumn(ColumnName,SearchText).ToList();
                    break;
                default:
                    query = db.Enquiries.OrderByDescending(i => i.Created_date).Select(i => new
                    {
                        Ref_no = i.Ref_no,
                        Name = db.Parties.Where(p => p.Id.Equals(i.party_id)).Select(p => p.Name).SingleOrDefault(),
                        StatusName = db.Status.Where(s => s.Id.Equals(i.status_id)).Select(s => s.StatusName).SingleOrDefault(),
                        CategoryName = db.Categories.Where(c => c.Id.Equals(i.category_id)).Select(c => c.category_name).SingleOrDefault(),
                        IsUregent = i.IsUregent,
                        Created_date = i.Created_date
                    }).FilterForColumn(ColumnName, SearchText).ToList();
                    break;
            }
            int count = 0;
            DataSet myDataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Ref_no", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("StatusName", typeof(int)));
            dt.Columns.Add(new DataColumn("CategoryName", typeof(string)));
            dt.Columns.Add(new DataColumn("IsUregent", typeof(bool)));
            dt.Columns.Add(new DataColumn("Created_date", typeof(DateTime)));
            foreach (var item in query)
            {
                if (item != null)
                {
                    DataRow dr = dt.NewRow();
                    dr["Ref_no"] = item.//not founding field that
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

.FilterForColumn(ColumnName,SearchText)的返回类型是什么? 如果只是过滤,而不更改Data Type,我认为你可以使用它:

var query = Enumerable.Repeat(new
        {
            Ref_no = string.Empty,
            Name = string.Empty,
            StatusName = default(int),
            CategoryName = string.Empty,
            IsUregent = default(bool),
            Created_date = default(DateTime)
        }, 0)
        .ToList();

而不是:

var query = (dynamic)null;

可能这可以解决您的问题