Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用LINQ内联选择_Linq_Tsql_Linq To Entities - Fatal编程技术网

使用LINQ内联选择

使用LINQ内联选择,linq,tsql,linq-to-entities,Linq,Tsql,Linq To Entities,我需要使用LINQ复制具有内部select的T-SQL语句: SELECT *, CustomerTypes.Description as CustomerType, (Select Min(Distinct DocumentStatistics.StatDateTime) As LastStatCheck From DocumentStatistics Where DocumentStatistics.CustomerId = Customers.CustomerId) As LastSt

我需要使用LINQ复制具有内部select的T-SQL语句:

SELECT *, 
CustomerTypes.Description as CustomerType, 
(Select Min(Distinct DocumentStatistics.StatDateTime) As LastStatCheck
From DocumentStatistics
Where DocumentStatistics.CustomerId = Customers.CustomerId) As LastStatCheck 
FROM Customers 
INNER JOIN CustomerTypes ON Customers.CustomerTypeID = CustomerTypes.CustomerTypeID 
ORDER BY CustomerName
下面是我能得到的最接近的数据,但它最终返回了所有的文档统计数据,40000多个

            Dim docQuery = (From doc In data.DocumentStatistics _
                       Select doc)

        Dim query = From customer In data.Customers _
                    Join docStat In docQuery _
                    On customer.CustomerID Equals docStat.CustomerID _
                    Select Customer = customer, LastStatCheck = _
                        (From doc In docQuery Where customer.CustomerID = doc.CustomerID _
                        Select doc.StatDateTime).Distinct().Min()
我错过了什么?如何复制初始SQL语句

我可以举VB或C的例子,我正在使用SQL Server数据库。

John

查看名为Linqer的产品,我与此产品没有关联。它接受SQL语句并将其转换为LINQ。它能够转换几乎任何你抛出的查询。这对我很有用。它也教会了我很多关于LINQ的工作原理

兰迪

约翰

查看名为Linqer的产品,我与此产品没有关联。它接受SQL语句并将其转换为LINQ。它能够转换几乎任何你抛出的查询。这对我很有用。它也教会了我很多关于LINQ的工作原理


兰迪

你真正想要的是加入一个团体

C例如:

var query = from c in data.Customers
            join ds in data.DocumentStatistics
            on c.CustomerID equals ds.CustomerID into stats
            select new
            {
                Customer = c,
                LastStatCheck = stats.Min(s => s.StatDateTime)
            };

你真正想要的是加入一个团体

C例如:

var query = from c in data.Customers
            join ds in data.DocumentStatistics
            on c.CustomerID equals ds.CustomerID into stats
            select new
            {
                Customer = c,
                LastStatCheck = stats.Min(s => s.StatDateTime)
            };

您需要使用Group Join函数来实现这一点,类似于C中的以下内容:

var result = from customer in data.Customers
             join docStat in data.DocumentStatistics
                    on customer.CustomerID equals docStat.CustomerID into stats
             where stats.Count() > 0
             select new
             {
                 Customer = customer,
                 LastStatCheck = stats.Min(res => res.StatDateTime)
             };
在VB.Net中

Dim result = From customer In data.Customers _
             Group Join docStat In data.DocumentStatistics _
                  On customer.CustomerID Equals docStat.CustomerID Into stats = Group _
             Where stats.Count() > 0 _
             Select New With _
             { _
                 .Customer = customer _
                 .LastStatCheck = stats.Min(Function(res) res.StatDateTime) _
             }

您需要使用Group Join函数来实现这一点,类似于C中的以下内容:

var result = from customer in data.Customers
             join docStat in data.DocumentStatistics
                    on customer.CustomerID equals docStat.CustomerID into stats
             where stats.Count() > 0
             select new
             {
                 Customer = customer,
                 LastStatCheck = stats.Min(res => res.StatDateTime)
             };
在VB.Net中

Dim result = From customer In data.Customers _
             Group Join docStat In data.DocumentStatistics _
                  On customer.CustomerID Equals docStat.CustomerID Into stats = Group _
             Where stats.Count() > 0 _
             Select New With _
             { _
                 .Customer = customer _
                 .LastStatCheck = stats.Min(Function(res) res.StatDateTime) _
             }

Linqer根据我的原始SQL语句给了我以下结果:

From customer In data.Customers _
                    Select _
                    Customer = customer, _
                    LastStatCheck = (CType((Aggregate lastDateChecked In _
                    (From docStat In data.DocumentStatistics _
                    Where docStat.CustomerID = customer.CustomerID _
                    Select docStat) Into Min(lastDateChecked.StatDateTime)), DateTime?))

Linqer的有用之处在于,我可以将t-SQL语句放入并在LINQ语句旁边运行查询。

Linqer从我的原始SQL语句中给出了以下结果:

From customer In data.Customers _
                    Select _
                    Customer = customer, _
                    LastStatCheck = (CType((Aggregate lastDateChecked In _
                    (From docStat In data.DocumentStatistics _
                    Where docStat.CustomerID = customer.CustomerID _
                    Select docStat) Into Min(lastDateChecked.StatDateTime)), DateTime?))

Linqer的有用之处在于,我可以将t-SQL语句放入并在LINQ语句旁边运行查询。

感谢您的回复,Simon。可查询名称统计数据的初始化中似乎有错误。尝试在其他地方实例化它,但我不确定创建它的正确过程。你对如何初始化统计数据有什么建议吗?嗨,约翰,我错过了分组结束时的“=组”。以上内容现在应该可以按要求工作了……感谢您的回复,西蒙。可查询名称统计数据的初始化中似乎有错误。尝试在其他地方实例化它,但我不确定创建它的正确过程。你对如何初始化统计数据有什么建议吗?嗨,约翰,我错过了分组结束时的“=组”。以上内容现在应该可以按要求工作了…感谢Randy的建议,Linqer给了我我需要的答案。感谢Randy的建议,Linqer给了我我需要的答案。我之前没有看到聚合关键字,所以进行了一些搜索,发现这可能需要小心,我已经更新了我的答案,现在它应该做你想做的事情,而不使用聚合关键字…阅读本文后,我同意。在使用聚合关键字时,似乎存在引入性能缺陷的风险。虽然这种风险似乎很小,但如果我能通过不冒险来完成任务,那就更好了。谢谢你的研究。我以前没有看到聚合关键字,所以做了一些搜索,发现这可能需要小心,我已经更新了我的答案,现在应该做你想做的事情,而不使用聚合关键字…阅读本文后,我同意。在使用聚合关键字时,似乎存在引入性能缺陷的风险。虽然这种风险似乎很小,但如果我能通过不冒险来完成任务,那就更好了。谢谢你的研究。