简体   繁体   中英

Retrieving Data From Multiple Tables in a LINQ to SQL query

I'm new to LINQ and I'm not sure how to retrieve data from multiple tables from my SQL server database, here's the query:

SELECT cp.*, tsd.Action, tsd.CurrencyPair 
from TradeStatsData tsd, CalculatedPrices cp 
where tsd.TradeID = cp.TradeID and cp.Price is null and cp.ActiveTime < GETDATE()

The database uses the variable connection

How can I do this?

Your sql query would be something like this in LINQ:

var result = from tsd in TradeStatsData
             join cp in CalculatedPrices on tsd.TradeID equals cp.TradeID
             where cp.Price == null && cp.ActiveTime < DateTime.Now
             select new
             {
                CP = cp,
                Action = tsd.Action,
                CurrencyPair = tsd.CurrencyPair
             };

Linq is very similar to sql, just a little backwards.

from tsd in TradeStatsData
join cp in CalculatedPrices on tsd.TradeID equals cp.TradeID
where cp.Price == null && cp.ActiveTime < DateTime.Now
select new { cp.Col1... cp.ColN, tsp.Action, tsp.CurrencyPair }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM