将SQL转换为Linq

时间:2011-07-25 08:48:29

标签: c# linq lambda

我正在使用新的实体框架,只是为了好玩,我想将旧网站的所有现存查询转换为新的和新颖的linq。但我真的无法解决这个问题。最小的帮助会很棒。

我尝试转换的查询..现在可以处理我的c#代码中的foreach,所以如果它离开了,那就更多的是嵌套选择和IN就是在困扰我

DECLARE @Begin DateTime, @End DateTime, @date DateTime, @partnerId int

Set @partnerId = 1
Set @Begin = Cast('2010-01-01' as DateTime)
Set @End = Cast(Dateadd(month, 17, @Begin) as DateTime)
Set @date = @Begin

SET NOCOUNT ON
WHILE (@date <= @End)
BEGIN
    SELECT
        [PackageId],
        @date AS [Date],
        [Name],
        (ISNULL((SELECT [ReportId] FROM [Uniferon_Reports] WHERE [FK_PartnerId] = @partnerId AND [FK_PackageId] = [Uniferon_Packages].[PackageId] AND [Date] = @date), 0)) AS [ReportId],
        (ISNULL((SELECT [UnitsForecast] FROM [Uniferon_Reports] WHERE [FK_PartnerId] = @partnerId AND [FK_PackageId] = [Uniferon_Packages].[PackageId] AND [Date] = @date), 0)) AS [UnitsForecast],
        (ISNULL((SELECT [UnitsActual] FROM [Uniferon_Reports] WHERE [FK_PartnerId] = @partnerId AND [FK_PackageId] = [Uniferon_Packages].[PackageId] AND [Date] = @date), 0)) AS [UnitsActual],
        (ISNULL((SELECT [RevenueForecast] FROM [Uniferon_Reports] WHERE [FK_PartnerId] = @partnerId AND [FK_PackageId] = [Uniferon_Packages].[PackageId] AND [Date] = @date), 0)) AS [RevenueForecast],
        (ISNULL((SELECT [RevenueActual] FROM [Uniferon_Reports] WHERE [FK_PartnerId] = @partnerId AND [FK_PackageId] = [Uniferon_Packages].[PackageId] AND [Date] = @date), 0)) AS [RevenueActual],
        (ISNULL((SELECT [UnitsInStock] FROM [Uniferon_Reports] WHERE [FK_PartnerId] = @partnerId AND [FK_PackageId] = [Uniferon_Packages].[PackageId] AND [Date] = @date), 0)) AS [UnitsInStock]
        FROM
            [Uniferon_Packages][Uniferon_Packages]
        WHERE
        [PackageId] IN 
        (SELECT [FK_PackageId] FROM [Uniferon_Partners_Packages_Relation] WHERE [FK_PartnerId] = @partnerId AND [StartDate] <= @date    AND ([EndDate] >= @date OR [EndDate] IS NULL))


Set @date = DateAdd(month, 1, @date)
END
SET NOCOUNT OFF   

任何想法?

4 个答案:

答案 0 :(得分:1)

有一个名为Linqer的工具,它可以将您的SQL语句转换为lamba函数。放手一搏,看看它能产生什么。

答案 1 :(得分:0)

我认为首先要做的是尝试理解原始SQL - 然后你可以看看在Linq中编写查询。

简单地看一下,它看起来像原始查询的SELECT本身可能是一个写得不好的外连接?可能值得一看......

但是,请记住,如果在SQL中确实需要WHILE,那么您可能无法有效地将其移植到客户端C#代码 - 如果需要数据库上的循环那么它可能更多最好将此代码保留在服务器端SQL中。

答案 2 :(得分:0)

像这样的东西。希望这能帮助您更好地理解LINQ;):

while (date <= endDate)
{
    var result = Uniferon_Packages
        .Where(i => 
            Uniferon_Partners_Packages_Relation.Any(rel => rel.FK_PartnerId == i.PackageId) &&
            rel.StartDate <= date && (!rel.EndDate.HasValue || rel.EndDate >= date))
        .Select(i => new
                         {
                             i.PackageId,
                             i.Name,
                             Date = date,
                             ReportId = GetReportId(i, partnerId, date)
                             // etc...
                         });

    date = date.AddMonths(1);
}


private int GetReportId(Uniferon_Package pack, int partnerId, DateTime date)
{
    var report = Uniferon_Reports.FirstOrDefault(x => 
        x.FK_PartnerId == partnerId && 
        x.FK_PackageId == pack.PackageId && 
        x.Date == date);

    return report != null
        ? report.ReportId
        : 0;
}

答案 3 :(得分:0)

您可以查看LINQPad,它可以帮助您学习LINQ并检查您的LINQ表达是否正确。