Solved

Silverlight WCF RIA - conditional EntityQuery

Posted on 2012-03-21
2
813 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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