开发者

How to perform LINQ To SQL for Database in Windows Phone

开发者 https://www.devze.com 2023-03-20 16:45 出处:网络
Say, I have the following tables in SQLCe Database: Table CountryT开发者_C百科able Cities ------------------------

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 :

  1. How do I select using normal Select-statement : Select cityname where country = USA?

  2. 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
        };
0

精彩评论

暂无评论...
验证码 换一张
取 消