Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create dynamic linq queries?

Status
Not open for further replies.

elhaix

Programmer
Jul 31, 2006
115
CA
The article found does a good job of describing the problem - dynamically adding where parameters to Linq queries, based on some text field.

I managed to work around this with the following queries, but have since added an additional filtering field, which would make the most sense to add dynamically, instead of rewriting each query:




1 IEnumerable dataImport = from ds in matches
2 orderby ds.FileUrl ascending
3 where ds.CampaignID == Convert.ToInt32(Session["CampaignID"].ToString())
4 select ds;
5
6
7 // get TLP, CLP, or PLP
8 if (DDPageType.SelectedValue != "ALL" & DDPageType.SelectedValue != "UNDEFINED")
9 {
10 dataImport = from ds in db.tbl_DataImportMasterLists
11 orderby ds.FileUrl ascending
12 where ds.CampaignID == Convert.ToInt32(Session["CampaignID"].ToString())
13 && ds.PageType.ToUpper() == DDPageType.SelectedValue
14 select ds;
15 }
16
17 if (DDPageType.SelectedValue == "UNDEFINED")
18 {
19 dataImport = from ds in db.tbl_DataImportMasterLists
20 orderby ds.FileUrl ascending
21 where ds.CampaignID == Convert.ToInt32(Session["CampaignID"].ToString())
22 && (ds.PageType.ToUpper() != "TLP"
23 && ds.PageType.ToUpper() != "CLP"
24 && ds.PageType.ToUpper() != "PLP")
25 select ds;
26 }
27
28 //
29 // Retrieving data from DataImportMasterList
30 //
31 foreach (tbl_DataImportMasterList ds in dataImport)
32 {
33 // build a page object
34 pageObject.FileUrl = ds.FileUrl;
35 pageObject.FileUrlDisplay = DisplayFileUrl(ds.FileUrl, ds.PageType); // send page type for hover text
36 pageObject.TrackUrl = ds.TrackUrl;
37 .
38 .
39 .
40 // add it to an ArrayList
41 arrUlrList.Add(pageObject);
42
43 }//foreach



Basically I use IEnumerable to create a data object I can loop through for results. The appropriate query is selected based on some on-page criteria (easy to see).

Now, I want to add a "CONTAINS" filter, based on a new text field on my form:

1 IQueryable matches = db.tbl_DataImportMasterChanges;
2 if (TxtStringSearch.Text.Length > 0)
3 {
4 matches = matches.Where(row => row.Title.ToLower().Contains(TxtStringSearch.Text.ToLower().Trim()));
5 }



However, in line 1, at db, I get the following error:

Cannot implicitly convert type 'System.Data.Linq.Table<tbl_DataImportMasterChange>' to 'System.Linq.IQueryable<tbl_DataImportMasterList>'

DataClassesDataContext db is an instance of my Linq-generated DataClasses.dbml.


So given this, the question is, how to dynamically create a linq query with (not only CONTAINS), but ORDER BY, etc, and be able to iterate through the results?



Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top