我有一些非常简单的 SQL,我可以尝试将其更改为 Linq 或 LLBLGEN

发布于 2024-09-29 14:30:01 字数 3144 浏览 4 评论 0原文

我想做这样的事情...

SELECT DISTINCT T1.* 
FROM T1 
INNER JOIN T2 ON T2.ID1 = T1.ID1
INNER JOIN T3 ON T3.ID2 = T2.ID2
--FOLLOWING CAN BE ADDED MULTIPLE TIMES  (LOOPS IN C#?)
INNER JOIN T2 AS T2A ON T3.ID2 = T2A.ID2
INNER JOIN T1 AS T1A ON T1A.ID1 = T2A.ID1
--END MULTI
WHERE T1.ID1 = 1
AND T3.ID3 = 2
AND T3.ID4 = 3
--THE FOLLOWING CONDITIONS WILL ALSO BE FOR EVERY SET OF EXTRA JOINS (LOOPS IN C#?)
AND T1A.ID1 = 4
AND T1I.ID5 = 5
--END MULTI

...在 Linq 或 LLBLGen 代码中。任何帮助将不胜感激!

这是我迄今为止拥有的 LLBGen...

            IPredicateExpression filter = new PredicateExpression();
            filter.Add(ProductTypeOptionAttributeFields.OptionId == dl.Key);
            filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductTypeId == DataSource.DataItem.ProductTypeId);
            filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductId == DataSource.ProductID);
            bucket.PredicateExpression.Add(filter);

            bucket.Relations.Add(ProductTypeOptionAttributeEntity.Relations.ProductTypeOptionAttributeCombinationProfileEntityUsingProductTypeOptionAttributeId, JoinHint.Inner);
            bucket.Relations.Add(ProductTypeOptionAttributeCombinationProfileEntity.Relations.ProductTypeOptionAttributeCombinationEntityUsingProductTypeOptionAttributeCombinationId, JoinHint.Inner);

            var filtered = _dropdowns.Where(k => ((DropDownList)k.Value[1]).SelectedValue != "-1" && k.Key != dl.Key);
            foreach (var filteredDdl in filtered)
            {

                IPredicateExpression subFilter = new PredicateExpression();
                subFilter.AddWithAnd(ProductTypeOptionAttributeFields.AttributeId == int.Parse(((DropDownList)filteredDdl.Value[1]).SelectedValue));
                subFilter.AddWithAnd(ProductTypeOptionAttributeFields.OptionId == filteredDdl.Key);
                bucket.PredicateExpression.AddWithAnd(subFilter);                    
            }


            ProductTypeOptionAttributeCollection attrs = new ProductTypeOptionAttributeCollection();
            attrs.GetMulti(bucket.PredicateExpression, -1, null, bucket.Relations);

这是我想要的实际查询...

    SELECT DISTINCT PTOA.* 
    FROM ProductTypeOptionAttribute AS PTOA
    INNER JOIN ProductTypeOPtionAttributeCombinationProfile AS PTOACP ON PTOACP.ProductTypeOPtionAttributeID = PTOA.AttributeID
    INNER JOIN ProductTypeOPtionAttributeCombination AS PTOAC ON PTOAC.CombinationID = PTOACP.ProductTypeOptionAttributeCombinationID
    --FOLLOWING CAN BE ADDED MULTIPLE TIMES  (LOOPS IN C#?)
    INNER JOIN ProductTypeOPtionAttributeCombinationProfile AS PTOACP2 ON PTOAC.CombinationID = PTOACP2.ProductTypeOptionAttributeCombinationID
    INNER JOIN ProductTypeOPtionAttribute AS PTOA2 ON PTOACP2.ProductTypeOPtionAttributeID = PTOA2.AttributeID
--END MULTI
    WHERE PTOA.OptionID = 59
    AND PTOAC.ProductTypeID = 11

AND PTOAC.ProductID = 218
--THE FOLLOWING CONDITIONS WILL ALSO BE FOR EVERY SET OF EXTRA JOINS (LOOPS IN C#?)
AND PTOA2.AttributeID = 42
AND PTOA2.OptionID = 58
--END MULTI

干杯

I want to do something like this...

SELECT DISTINCT T1.* 
FROM T1 
INNER JOIN T2 ON T2.ID1 = T1.ID1
INNER JOIN T3 ON T3.ID2 = T2.ID2
--FOLLOWING CAN BE ADDED MULTIPLE TIMES  (LOOPS IN C#?)
INNER JOIN T2 AS T2A ON T3.ID2 = T2A.ID2
INNER JOIN T1 AS T1A ON T1A.ID1 = T2A.ID1
--END MULTI
WHERE T1.ID1 = 1
AND T3.ID3 = 2
AND T3.ID4 = 3
--THE FOLLOWING CONDITIONS WILL ALSO BE FOR EVERY SET OF EXTRA JOINS (LOOPS IN C#?)
AND T1A.ID1 = 4
AND T1I.ID5 = 5
--END MULTI

...in either Linq or LLBLGen Code. Any help would be greatly appreciated!

Here is the LLBGen I have so far...

            IPredicateExpression filter = new PredicateExpression();
            filter.Add(ProductTypeOptionAttributeFields.OptionId == dl.Key);
            filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductTypeId == DataSource.DataItem.ProductTypeId);
            filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductId == DataSource.ProductID);
            bucket.PredicateExpression.Add(filter);

            bucket.Relations.Add(ProductTypeOptionAttributeEntity.Relations.ProductTypeOptionAttributeCombinationProfileEntityUsingProductTypeOptionAttributeId, JoinHint.Inner);
            bucket.Relations.Add(ProductTypeOptionAttributeCombinationProfileEntity.Relations.ProductTypeOptionAttributeCombinationEntityUsingProductTypeOptionAttributeCombinationId, JoinHint.Inner);

            var filtered = _dropdowns.Where(k => ((DropDownList)k.Value[1]).SelectedValue != "-1" && k.Key != dl.Key);
            foreach (var filteredDdl in filtered)
            {

                IPredicateExpression subFilter = new PredicateExpression();
                subFilter.AddWithAnd(ProductTypeOptionAttributeFields.AttributeId == int.Parse(((DropDownList)filteredDdl.Value[1]).SelectedValue));
                subFilter.AddWithAnd(ProductTypeOptionAttributeFields.OptionId == filteredDdl.Key);
                bucket.PredicateExpression.AddWithAnd(subFilter);                    
            }


            ProductTypeOptionAttributeCollection attrs = new ProductTypeOptionAttributeCollection();
            attrs.GetMulti(bucket.PredicateExpression, -1, null, bucket.Relations);

And here is the actual query I want...

    SELECT DISTINCT PTOA.* 
    FROM ProductTypeOptionAttribute AS PTOA
    INNER JOIN ProductTypeOPtionAttributeCombinationProfile AS PTOACP ON PTOACP.ProductTypeOPtionAttributeID = PTOA.AttributeID
    INNER JOIN ProductTypeOPtionAttributeCombination AS PTOAC ON PTOAC.CombinationID = PTOACP.ProductTypeOptionAttributeCombinationID
    --FOLLOWING CAN BE ADDED MULTIPLE TIMES  (LOOPS IN C#?)
    INNER JOIN ProductTypeOPtionAttributeCombinationProfile AS PTOACP2 ON PTOAC.CombinationID = PTOACP2.ProductTypeOptionAttributeCombinationID
    INNER JOIN ProductTypeOPtionAttribute AS PTOA2 ON PTOACP2.ProductTypeOPtionAttributeID = PTOA2.AttributeID
--END MULTI
    WHERE PTOA.OptionID = 59
    AND PTOAC.ProductTypeID = 11

AND PTOAC.ProductID = 218
--THE FOLLOWING CONDITIONS WILL ALSO BE FOR EVERY SET OF EXTRA JOINS (LOOPS IN C#?)
AND PTOA2.AttributeID = 42
AND PTOA2.OptionID = 58
--END MULTI

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-10-06 14:30:01

LLBLGen 提示:

  • 使用 SQL Server Profiler 查看发出的 SQL(在调用 GetMulti 后立即设置断点,然后观察跟踪)
  • 您有很多复杂的 UI 登录和转换/转换可能会失败 - 我个人的偏好是将它们移动到单独的代码中
  • 您不需要 JoinHint.Inner,因为这是默认设置
  • (个人偏好),使用 RelationCollection 而不是存储桶。

我不完全理解你的情况(特别是多个连接到同一个表?),但这可能有效。我认为你想要 subFilter.AddWithOr 而不是 .AddWithAnd。

    IPredicateExpression filter = new PredicateExpression();
    filter.Add(ProductTypeOptionAttributeFields.OptionId == dl.Key);
    filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductTypeId == DataSource.DataItem.ProductTypeId);
    filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductId == DataSource.ProductID);

    IRelationCollection relations = new RelationCollection();
    relations.Add(ProductTypeOptionAttributeEntity.Relations.ProductTypeOptionAttributeCombinationProfileEntityUsingProductTypeOptionAttributeId);
    relations.Add(ProductTypeOptionAttributeCombinationProfileEntity.Relations.ProductTypeOptionAttributeCombinationEntityUsingProductTypeOptionAttributeCombinationId);

    var filtered = _dropdowns.Where(k => ((DropDownList)k.Value[1]).SelectedValue != "-1" && k.Key != dl.Key);
    foreach (var filteredDdl in filtered)
    {
        IPredicateExpression subFilter = new PredicateExpression();
        subFilter.AddWithOr(ProductTypeOptionAttributeFields.AttributeId == int.Parse(((DropDownList)filteredDdl.Value[1]).SelectedValue));
        subFilter.AddWithOr(ProductTypeOptionAttributeFields.OptionId == filteredDdl.Key);
        filter.AddWithAnd(subFilter);
    }

    ProductTypeOptionAttributeCollection attrs = new ProductTypeOptionAttributeCollection();
    attrs.GetMulti(filter, relations)

LLBLGen Tips:

  • Use SQL Server Profiler to view the SQL emitted (set a breakpoint right after your call to GetMulti, then watch your trace)
  • You have a lot of complicated UI login and cast/converts that could fail -- my personal preference would be to move those to separate code
  • You don't need JoinHint.Inner as that is the default
  • (Personal preference) use a RelationCollection instead of the bucket.

I don't entirely understand your situation (especially the multiple joins to the same table?), but this may work. I think you want subFilter.AddWithOr instead of .AddWithAnd.

    IPredicateExpression filter = new PredicateExpression();
    filter.Add(ProductTypeOptionAttributeFields.OptionId == dl.Key);
    filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductTypeId == DataSource.DataItem.ProductTypeId);
    filter.AddWithAnd(ProductTypeOptionAttributeCombinationFields.ProductId == DataSource.ProductID);

    IRelationCollection relations = new RelationCollection();
    relations.Add(ProductTypeOptionAttributeEntity.Relations.ProductTypeOptionAttributeCombinationProfileEntityUsingProductTypeOptionAttributeId);
    relations.Add(ProductTypeOptionAttributeCombinationProfileEntity.Relations.ProductTypeOptionAttributeCombinationEntityUsingProductTypeOptionAttributeCombinationId);

    var filtered = _dropdowns.Where(k => ((DropDownList)k.Value[1]).SelectedValue != "-1" && k.Key != dl.Key);
    foreach (var filteredDdl in filtered)
    {
        IPredicateExpression subFilter = new PredicateExpression();
        subFilter.AddWithOr(ProductTypeOptionAttributeFields.AttributeId == int.Parse(((DropDownList)filteredDdl.Value[1]).SelectedValue));
        subFilter.AddWithOr(ProductTypeOptionAttributeFields.OptionId == filteredDdl.Key);
        filter.AddWithAnd(subFilter);
    }

    ProductTypeOptionAttributeCollection attrs = new ProductTypeOptionAttributeCollection();
    attrs.GetMulti(filter, relations)
夜光 2024-10-06 14:30:01

您提供的基本 SQL 查询应该很难在 LINQ 中重现。

从 T1 中的 t
其中 T1.ID == 1
选择;

如果您还没有使用它,请下载免费的 LINQPAD http://www.linqpad.net/ 它有很多帮助您快速了解情况的示例。

The basic SQL query you presented should be faily easy to reproduce in LINQ.

From t in T1
Where T1.ID == 1
Select;

if your not already using it download the free LINQPAD http://www.linqpad.net/ Its got loads of examples to get you up to speed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文