Say, I have the following tables in SQLCe Database:
Table Country T开发者_C百科able Cities ----------- ------------- Pkey countryname Pkey Fkey cityname 1. USA 1. 1. LA 2. Canada 2. 2. Toronto 3. 1. NYC 4. 1. Chicago
Here the code to get the cities:
public IList GetCities(){ IList cityList = null; using (CountryDataContext context = new CountryDataContext(ConnectionString)) { IQueryable query = from c in context.Cities select c; cityList = query.ToList(); } return cityList; }
Questions are :
How do I select using normal Select-statement : Select cityname where country = USA?
How do I select the country and get the primary key ? Select ID, Countryname
then, I can pass the Pkey and do a select again
Select cityname where Fkey = "1";
would apreciate any help or sample on LinqToSQL for normal Databse operation.
Thanks
This should get you your first question:
var query = from c in context.Cities
join y in context.Country
on c.Pkey equals y.Fkey
where y.countryname.Equals("USA")
select c.cityname;
I'm not sure I understand what you're asking in the second question, but I think this may be it:
var query = from c in context.Cities
join y in context.Country
on p.Pkey equals y.Fkey
where y.Pkey.Equals(1)
select new
{
c.Pkey,
c.cityname
};
精彩评论