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

how to query data/select from mssql

2 Answers 63 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Anthony
Top achievements
Rank 1
Anthony asked on 21 Aug 2014, 05:07 PM
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 Answers, 1 is accepted

Sort by
0
Anthony
Top achievements
Rank 1
answered on 21 Aug 2014, 05:15 PM
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?
0
Boris Georgiev
Telerik team
answered on 26 Aug 2014, 12:13 PM
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.
 
Tags
Data Access Free Edition
Asked by
Anthony
Top achievements
Rank 1
Answers by
Anthony
Top achievements
Rank 1
Boris Georgiev
Telerik team
Share this question
or