how to query data/select from mssql

3 posts, 0 answers
  1. Anthony
    Anthony avatar
    13 posts
    Member since:
    Aug 2014

    Posted 21 Aug 2014 Link to this post

    I'm confused about how to use a data context from a domain model. I'm following the sofia car rental example to build my own application, which works for the most part, but only the automated pieces. I don't know how to actually interact with the database using an API and I can't find anything like "here's how to select."

    How do you select data from a database? I've only been able to use the ToList method with any amount of success, but I don't understand how to do anything else.

    I created a domain model and I'm able to populate a grid, but I want to get lists of data to use for other purposes that won't be exposed directly to a UI element. How do I query, say, a distinct list of Cities from a table called CityCountyState with three columns (City, Count, State). I don't think I can make a string with a direct MsSQL query because I've seen a lot of examples suggesting that I have to use notation I don't understand (e.g., g => ...).
  2. Anthony
    Anthony avatar
    13 posts
    Member since:
    Aug 2014

    Posted 21 Aug 2014 in reply to Anthony Link to this post

    asdf

    1.public AddEditViewModel : base( new AddEditWindow() )
    2.{
    3.    this.context = new GeotechLogContext();  
    4.    string[] str = this.context.CityCountyStates.<what goes here?>;
    5.    //str should have a distinct list of cities ordered by [City]
    6.}


    Also, how do I query with a filter? Say I don't want to wait to query the whole list of cities because I know something ahead of time (an autocomplete box, for example). If I know the City starts with "Ab" and the County has the letters "tr" somewhere in the string, how do I query that instead of the entire list?
  3. DevCraft banner
  4. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 26 Aug 2014 Link to this post

    Hi Anthony,

    Through the Domain Model, Telerik Data Access exposes entities as objects and with the support of the LINQ by OpenAccessContext, you can write queries directly against the database. This means that if you want to select only a set of entities from the database which meet some criteria, you can use LINQ to the context endpoints and the select statement will be executed on the server.

    You can find my answers for each of the posted questions below:

    Q) How do I query, say, a distinct list of Cities from a table called CityCountyState with three columns (City, Count, State).

    A) If the domain class is named "CityContyState" and the endpoint in the context is CityContyStates:
    var cities = context.CityContyStates.Select(c => c.City).Distinct();

    Q) If I know the City starts with "Ab" and the County has the letters "tr" somewhere in the string, how do I query that instead of the entire list?

    A) 
    var result = context.CityContyStates.Where(c => c.City.StartsWith("Ab") && c.County.Contains("tr"));

    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top