none
Restrict LINQ Subquery when databound to a grid RRS feed

  • Question

  •  

    I want to limit the number of child elements I get back.  In this example Order.CustomerID "VINET" has 3 Order Details.  I only want to see the record that has a unit price of 14.  I do NOT want to see the Order Details where the unit price equals 9.8 or 43.8

    In the end I want to do this in a dynamic where query or with a predicate, but the simple example should show my problem. I tried this a couple of different ways including the two i have shown below. I realize the problem is in the fact that LINQ is automatically running its own queries when I expand, but does anyone have a good solution.

    private
    void btnJoinProblem_Click(object sender, EventArgs e)

    {

    NorthwindDataContext db = new NorthwindDataContext();

    var tempQ2 = (from od in db.Order_Details

    join o in db.Orders on od.OrderID equals o.OrderID

    where od.UnitPrice == 14

    select o).Distinct();


    var tempQ = (from o in db.Orders

    where db.Order_Details.Any(od => od.UnitPrice == 14)

    select o).Distinct();

    ultraGrid1.DataSource = tempQ;

    }


    joe alt
    Saturday, March 21, 2009 1:57 PM

Answers

  • The problem was the lazy loading in LINQ.  I had to implement my own custom objects.
    joe alt
    • Marked as answer by altja Tuesday, March 31, 2009 9:24 PM
    Tuesday, March 31, 2009 9:23 PM

All replies

  • For dynamic generation of DLINQ queries You can use Expression trees. link below will help you in building such expressions. Example given in link just deals with string array and fixed vales ,i.e expression is written assuming fixed values, so you will need to create one function which accepts your filter values as argument and then try to build expression tree using passed values for you query the way its described in link below,

    http://msdn.microsoft.com/en-us/library/bb882637.aspx


    Your final code will be something like this,


    var result = RunDynamicWhereQuery("UnitPrice", "14");


    private IQueryable RunDynamicWhereQuery(string field,string filter)

    {

     //Expression Bulding code

    return results;

    }



    Hope this helps you.
    • Proposed as answer by Jatinp Sunday, March 22, 2009 5:15 PM
    Sunday, March 22, 2009 5:09 PM
  • I appreciate your responce, but I don't think this is going to solve my problem.  My problem is not getting back the proper data, but getting back the proper data when I expand a row.  I think LINQ is runnning its own queries to get me the subitems.

    Could you show me how an expression tree for the example above would work even if it is hard coded as an expression tree.
    joe alt
    Monday, March 23, 2009 11:37 AM
  • The problem was the lazy loading in LINQ.  I had to implement my own custom objects.
    joe alt
    • Marked as answer by altja Tuesday, March 31, 2009 9:24 PM
    Tuesday, March 31, 2009 9:23 PM