c# - 在 LINQ to SQL 查询中从多个表检索数据

标签 c# sql linq linq-to-sql

我是 LINQ 新手,不确定如何从 SQL Server 数据库的多个表中检索数据,查询如下:

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()

数据库使用变量连接

我该怎么做?

最佳答案

您的 sql 查询在 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
             };

关于c# - 在 LINQ to SQL 查询中从多个表检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21588468/

相关文章:

c# - 使用 String.Substrings,是否缺乏性能?

sql - 您将如何在您首选的 db 范式中为您的模型实现修订控制系统?

mysql - 如何搜索所有表记录,如果不存在则创建一个新的空记录?

c# - 比较整数列表,然后根据相似性对它们进行排序。

c# - 如何在循环中使用 linq?

c# - 如何按日期排序前 10 个不同的行

c# - DNX/.NET Core 程序集版本控制 : 4. 0.0 与 4.0.10

c# - Xamarin.Forms:导航到页面失败

c# - 通过 css 类分配 ASP.net 按钮图像

mysql - 在同一个sql中返回字段和计数