Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Silverlight WCF RIA - conditional EntityQuery

Posted on 2012-03-21
2
Medium Priority
?
828 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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This tutorial will show you how to add an attribute to an XML (http://en.wikipedia.org/wiki/XML) stream returned from a Windows Communication Foundation (http://en.wikipedia.org/wiki/Windows_Communication_Foundation) (WCF) Web Service.  Some knowled…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

604 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