Solved

Silverlight WCF RIA - conditional EntityQuery

Posted on 2012-03-21
2
809 Views
Last Modified: 2012-03-23
I have a form with 2 comboboxes.  A user selects an item in the first combobox and the second one populates based on the selection in the first one.  When a button is clicked, a query is run, based on the selections, and the results are displayed in the grid.  I'm using this code to populate the grid, and it works fine:

int productid = 0; 
            int itemid = 0;
            Web.Product product = new Web.Product();
            Web.Item item = new Web.Item();

            BusinessApplication1.Web.DomainService1 context = new Web.DomainService1();

            if (comboBox1.SelectedIndex != -1)
            {
                product = (Web.Product)comboBox1.SelectedItem;
                productid = product.ProductID;
            }
            

            if (comboBox2.SelectedIndex != -1)
            {
                item = (Web.Item)comboBox2.SelectedItem;
                itemid = item.ItemID;
            }

            string qstr = "from c in context.GetIssuesQuery() where";
            if (productid != 0) qstr += " c.IssueProduct.Value == " + productid;
            if (itemid != 0) qstr += " && c.IssueItem.Value ==" + itemid;
            qstr += " select c";

            
            EntityQuery<Web.Issue> query = from c in context.GetIssuesQuery() where c.IssueProduct.Value == productid && c.IssueItem.Value == itemid select c;
            LoadOperation<Web.Issue> itemqueryop = context.Load(query);

            dataGrid1.ItemsSource = itemqueryop.Entities;

Open in new window


  Where I'm stuck is that if the user makes no selection in both comboboxes (both values are zero), I want all entities in the table returned, and if they select an item in the first box but not the second, all entities with that product ID are returned.  How do I create the query to do this?  Currently, it is working great if the user makes a selection in both boxes.  I'm new to Silverlight and am more of an SQL query guy, so any help would be much appreciated!
0
Comment
Question by:cmerlo1
2 Comments
 
LVL 14

Accepted Solution

by:
Vel Eous earned 500 total points
ID: 37754396
Assuming that your conditional checking on your combobox controls is working, you could alter how you are producing your LINQ query.

I.E.
1. Produce the basic from context statement
2. Check if you have a valid productid, if so add a where clause, otherwise do nothing
3. Check if you have a valid itemid, if so check if your query already has the where clause, if so add an and clause, otherwise add a where clause, otherwise add nothing
4. Add your select context

So you would basically be adding either a where, or and only if there was something to add and nothing had already been added to your basic context statement.

The following might work for you.

// create basic from context
string qstr = "from c in context.GetIssuesQuery()"
// is there a valid productid and if so, add a where clause, otherwise add nothing
qstr += productid > 0 ? " where c.IssueProduct.Value == " + productid : "";
// is there a valid itemid and if so, is there already a where clause, if so add an add clause, otherwise add a where clause, otherwise add nothing
qstr += > 0 ? qstr.IndexOf("where") != -1 ? " && c.IssueItem.Value == " itemid : " where c.IssueItem.Value == " + itemid : "";
// add the select context
qstr += " select c"

Open in new window


It might be nicer and easier to read with if statements rather than ternary statements.
0
 

Author Closing Comment

by:cmerlo1
ID: 37757112
I realized I posted the wrong code just now- the string was something I was attempting, and it didn't work.  I did figure it out, based on your suggestion:

EntityQuery<Web.Issue> query = from c in context.GetIssuesQuery() select c;

            if (productid != 0) query.Where(c => c.IssueProduct == productid);
            if (itemid != 0) query.Where(c => c.IssueItem == itemid);

Open in new window


This is working- thanks for your help in getting me headed in the right direction.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question