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

Selecting few columns from a table

1 Answer 61 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sathish
Top achievements
Rank 1
Sathish asked on 27 Feb 2009, 03:01 PM
Hi,

I have about 50 fields in a table called "Products". I need to search the DB for matching search criteria and retrieve only 10 columns as I need to display only 10 for search results (i.e to make the search and display faster).

When I execute the query below, it works fine. But, what I WANT is SELECT o.ProductID, o.Name, o.DisplayName, o.Description, o.UPC.
        query = os.GetOqlQuery(Of Product)("SELECT * FROM ProductExtent o WHERE " & _ 
                                            "o.Name LIKE ""*" & searchFor & "*"" " & _ 
                                            "OR o.DisplayName LIKE '*" & searchFor & "*' " & _ 
                                            "OR o.Description LIKE '*" & searchFor & "*' " & _ 
                                            "OR o.Upc LIKE '*" & searchFor & "*' " & _ 
                                            "OR o.NotesUsage LIKE '*" & searchFor & "*' " & _ 
                                            "OR o.NotesMaintenance LIKE '*" & searchFor & "*' " & _ 
                                            "OR o.Description LIKE '*" & searchFor & "*' "

How can I do that? (please answer asap and I need to implement this in many places)

1 Answer, 1 is accepted

Sort by
0
Accepted
PetarP
Telerik team
answered on 27 Feb 2009, 03:46 PM
Hi Sathish,
I have simplified your query so it can be easily understandable. For the following example I have used the Northwind DataBase and the Employee table
IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); 
            string query = "Select o.EmployeeID, o.BirthDate from EmployeeExtent as o where o.EmployeeID =1"
            IQueryResult result = scope.GetOqlQuery(query).Execute(); 
            foreach (object[] em in result) 
            { 
                Console.WriteLine(em[0]); 
            } 
This query will only select employees ID and Birth Date. Be aware that when you do this the results records cannot be cast to Employee because they don't contain all the fields so you can access them as object items. Each item in result set is represent as array of objects. In the following case the em will have 2 fields, the first one will have the ID as value and the second will have the Birth date for its value.


Sincerely yours,
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
Development (API, general questions)
Asked by
Sathish
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or