Question : IQueryable on datatable

Experts,
I have to build dynamic linq query in C# and need immediate help with code.  
This is the datatable and I have to add where clauses dynamically to it.  Please help:
DataTable results = (from d in ((DataSet)_MyDataset).Tables["Records"].AsEnumerable()
                             orderby d.Field("Name") ascending
                             where (d.Field(columnName) != null)
                             where d[columnName].ToString().ToLower().Contains(searchstr.ToLower())
                             select d).CopyToDataTable();

Answer : IQueryable on datatable

Finally I was able to add dynamic where condition and this code is working. attaching Method. thx.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
public ListItemCollection searchProject(ListItemCollection projList)
    {
        DataSet DSToReturn = new DataSet();       
        ListItemCollection returnItems = new ListItemCollection();
        ListItemCollection ResultItems = new ListItemCollection();
        DataTable results = (from d in ((DataSet)_MyDataset).Tables["Records"].AsEnumerable()
                             orderby d.Field("Name") ascending
                             select d).CopyToDataTable();
        foreach (DataRow row in results.Rows)
        {
            ListItem item = new ListItem(row["Name"].ToString(), row["Value"].ToString());
            ResultItems.Add(item);
        }
        
     
       
        
        if (this.myItems[0] != "--")
        {
 
            var query = (from q in results.AsEnumerable()
                         where q["Business"].ToString().Contains(this.myItems[0])
                         select q);
            if (query.Count() > 0)
                results = query.CopyToDataTable();
            else
                results.Rows.Clear();
 
            
            
        }
        if (this.myItems[1] != "--")
        {
 
            var query = (from q in results.AsEnumerable()
                         where q["Business_group"].ToString().Contains(this.myItems[1])
                         select q);
            if (query.Count() > 0)
                results = query.CopyToDataTable();
            else
                results.Rows.Clear();
 
        }
        if (this.myItems[2] != "--")
        {
 
            var query = (from q in results.AsEnumerable()
                         where q["Value_center"].ToString().Contains(this.myItems[2])
                         select q);
            if (query.Count() > 0)
                results = query.CopyToDataTable();
            else
                results.Rows.Clear();
 
        }
        if (this.myItems[3] != "--")
        {
 
           
            var query = (from q in results.AsEnumerable()
                         where q["Client_name"].ToString().Contains(this.myItems[3])
                         select q);
            if (query.Count() > 0)
                results = query.CopyToDataTable();
            else
                results.Rows.Clear();
 
        }
        if (this.myItems[4] != "--")
        {
 
            var query = (from q in results.AsEnumerable()
                         where q["Owner_name"].ToString().Contains(this.myItems[4])
                         select q);
            if (query.Count() > 0)
                results = query.CopyToDataTable();
            else
                results.Rows.Clear();           
            
 
        }
       
        if (results.Rows.Count > 0)
        {
            foreach (ListItem li in ResultItems)
            {
                if ((from System.Data.DataRow row in results.Rows
                     where li.Value.Equals(row["value"].ToString(), StringComparison.InvariantCultureIgnoreCase)
                     select row["value"]).Count() > 0)
                    returnItems.Add(li);
            }
        }
        else
        {
            ListItem item = new ListItem("No Results", "0");
            returnItems.Add(item);
        }
 
        return returnItems;      
       
        
    }
 
Random Solutions  
 
programming4us programming4us