This is a migrated thread and some comments may be shown as answers.

How to retrieve records based on field value?

3 Answers 75 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
chris_cf
Top achievements
Rank 2
chris_cf asked on 26 Apr 2009, 01:37 AM
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.



3 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 27 Apr 2009, 07:43 AM
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.
0
chris_cf
Top achievements
Rank 2
answered on 27 Apr 2009, 10:54 PM
Thanks for this. Can you give me a LINQ example?

Which one is better to use?
0
PetarP
Telerik team
answered on 28 Apr 2009, 11:07 AM
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.
Tags
General Discussions
Asked by
chris_cf
Top achievements
Rank 2
Answers by
PetarP
Telerik team
chris_cf
Top achievements
Rank 2
Share this question
or