gpt4 book ai didi

.net - 非常复杂的 LINQ(to SQL)查询示例

转载 作者:行者123 更新时间:2023-12-04 17:59:16 25 4
gpt4 key购买 nike

我们正在考虑为 ORMBattle.NET 添加更多 LINQ 测试,但没有更多想法。所有 LINQ 测试都在检查常见的 LINQ 功能:

  • 任何测试都必须通过 LINQ 到 IEnumerable
  • 对于任何测试,必须至少有一个 ORM,它通过(实际上它是否在@ ORMBattle 列出并不重要)。

  • 目前 LINQ 测试序列的目标是自动计算 LINQ 实现覆盖率分数。

    先决条件:
  • Code of tests for LINQ to SQL is here .这必须对已经涵盖的内容进行一些想象。
  • 其他工具的测试(实际上它们是由 this T4 template 生成的)是 here .

  • 如果您对可以在那里添加的内容有任何想法,请分享。我肯定会接受满足上述要求的任何 LINQ 查询示例,并且可能 - 一些与改进测试套件相关的好主意,可以实现(例如,如果您建议我们手动研究翻译质量,这不会工作,因为我们不能自动化)。

    最佳答案

  • Expression.Invoke对于子表达式;适用于 LINQ-to-SQL 和 LINQ-to-Objects,但不适用于 3.5SP1 中的 EF(对于 IEnumerable<T>,首先调用 .AsQueryable()):
        Expression<Func<Customer, bool>> pred1 = cust=>cust.Country=="UK";
    Expression<Func<Customer, bool>> pred2 = cust=>cust.Country=="France";
    var param = Expression.Parameter(typeof(Customer), "x");
    var final = Expression.Lambda<Func<Customer, bool>>(
    Expression.OrElse(
    Expression.Invoke(pred1, param),
    Expression.Invoke(pred2, param)
    ), param);
    using (var ctx = new DataClasses1DataContext())
    {
    ctx.Log = Console.Out;
    int ukPlusFrance = ctx.Customers.Count(final);
    }

    示例 LINQ-to-SQL 输出(EF 在 Spark 中爆炸):
    SELECT COUNT(*) AS [value]
    FROM [dbo].[Customers] AS [t0]
    WHERE ([t0].[Country] = @p0) OR ([t0].[Country] = @p1)
    -- @p0: Input NVarChar (Size = 2; Prec = 0; Scale = 0) [UK]
    -- @p1: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [France]
  • 没有往返的身份管理器短路 - 即
    var obj = ctx.Single(x=>x.Id == id);
    var obj = ctx.Where(x=>x.Id == id).Single();

    如果具有该身份的对象已经被物化并存储在身份管理器中,则不需要去数据库;也适用于 First , SingleOrDefault , FirstOrDefault .参见 LINQ-to-SQL(还有 herehere ;你可以通过附加到 .Log 来验证);例子:
    using (var ctx = new DataClasses1DataContext())
    {
    ctx.Log = Console.Out;
    var first = ctx.Customers.First();
    string id = first.CustomerID;
    Console.WriteLine("Any more trips?");
    var firstDup = ctx.Customers.First(x=>x.CustomerID==id);
    Console.WriteLine(ReferenceEquals(first, firstDup)); // true
    Console.WriteLine("Prove still attached");
    int count = ctx.Customers.Count();
    }

    日志输出只显示两次行程;一个用于第一次获取对象,一个用于计数;它还显示了实现器返回的相同对象引用:
    SELECT TOP (1) [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[
    ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t
    0].[Country], [t0].[Phone], [t0].[Fax]
    FROM [dbo].[Customers] AS [t0]
    -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.492
    6

    Any more trips?
    True <==== this is object reference equality, not "are there any more trips"
    Prove still attached
    SELECT COUNT(*) AS [value]
    FROM [dbo].[Customers] AS [t0]
    -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.492
    6
  • UDF 支持;对于也适用于 LINQ-to-Objects 的简单示例:
    partial class MyDataContext {
    [Function(Name="NEWID", IsComposable=true)]
    public Guid Random() { return Guid.NewGuid();}
    }

    然后通过 x => ctx.Random() 订购;例子:
    using (var ctx = new DataClasses1DataContext())
    {
    ctx.Log = Console.Out;
    var anyAtRandom = (from cust in ctx.Customers
    orderby ctx.Random()
    select cust).First();
    }

    带输出:
    SELECT TOP (1) [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[        ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
    FROM [dbo].[Customers] AS [t0]
    ORDER BY NEWID()
  • 如果我真的感觉很邪恶,recursive lambda ;可能不值得以任何方式支持这一点……同样,4.0 表达式 (DLR) 节点类型。
  • 关于.net - 非常复杂的 LINQ(to SQL)查询示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1870082/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com