Solved

Silverlight WCF RIA - conditional EntityQuery

Posted on 2012-03-21
2
812 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
In previous Articles, we have discussed how we can upload a file using .asmx web service and isolated storage space. Here, in continuation to the topic, I am going to discuss how we can use WCF for the same purpose. Steps: 1.Create the silverli…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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