Solved

Silverlight WCF RIA - conditional EntityQuery

Posted on 2012-03-21
2
806 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:
Tchuki 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now