How to retrieve records based on field value?

4 posts, 0 answers
  1. chris_cf
    chris_cf avatar
    53 posts
    Member since:
    Jul 2007

    Posted 25 Apr 2009 Link to this post

    I have a table with Employees. It has First Name, Last Name, Enabled columns. This Employees table has a one-to-many relationship with the Comapny table (Many employees to one company).

    How do I retrieve the number of employees that have Enabled set to true and the number of employees that have Enabled set to false? The count retrieved should be for a particular company.

    I can retrive the total number of employees using the following:

    lblTotalEmployees.Text = ((Company)scope.GetObjectById(Database.OID.ParseObjectId(typeof(Company), Profile.CompanyID.ToString()))).Employees.Count.ToString(); 

    I am new to ORMs in general as well as LINQ and OQL so I am unsure of the best way to do it.



  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 27 Apr 2009 Link to this post

    Hello chris_cf,
    we have used a similar to yours case where we have class OrderDetailOA. It has orderDetailID, discount, quantity and unitPrice columns. This OrderDetailOA table has a one-to-many relationship with the Order table (Many OrderDetails to one Order).
    Now if we would want to retrieve the number of Orders who have their Order Details with discount that equals 0 we would use an oql query like this:
    SELECT COUNT(ct) FROM  OrderOAExtent AS x, x.orderDetailsOA as ct where ct.discount = 0 group by x.OrderID 
    This will return an list of Integers representing the count of the Order Details for each Order that have discount equaling 0. You can take advantage of our OQL Query Browser and test all your oql in order to see what the result would be.
    All the best,
    PetarP
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. chris_cf
    chris_cf avatar
    53 posts
    Member since:
    Jul 2007

    Posted 27 Apr 2009 Link to this post

    Thanks for this. Can you give me a LINQ example?

    Which one is better to use?
  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 28 Apr 2009 Link to this post

    Hi chris_cf,
    Using the same classes, a LINQ query that will produce the same output would look like this:
    IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); 
                var result = from c in scope.Extent<OrderDetail>() 
                               where c.Discount == 0 
                               group c by c.OrderID into OrderCountGroup 
                               select OrderCountGroup.Count(); 
                foreach (var itemCount in result) 
                { 
                    Console.WriteLine(itemCount); 
                } 
    Both languages produces the same output so its up to you to choose which one you find more suitable. If you face any further difficulties please do not hesitate to contact us.

    Kind regards,
    PetarP
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top