Selecting few columns from a table

2 posts, 1 answers
  1. Sathish
    Sathish avatar
    25 posts
    Member since:
    Dec 2008

    Posted 27 Feb 2009 Link to this post

    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)
  2. Answer
    PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 27 Feb 2009 Link to this post

    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.
  3. DevCraft banner
Back to Top