【问题标题】:if exists statement in sql to linqsql to linq中的if存在语句
【发布时间】:2009-01-24 16:08:40
【问题描述】:

以下语句的 linq 等效项是什么?

IF NOT EXISTS(SELECT UserName FROM Users WHERE UserName='michael')
BEGIN
INSERT INTO Users (UserName) values ('michael');
END

你能推荐任何 sql-to-linq 转换器吗?我目前正在使用 LINQPad,它在编写 linq 代码方面做得很好,您还可以看到生成的 sql 代码,但是当我单击小 linq 符号时,什么都没有显示。

【问题讨论】:

  • 我也一直在寻找 SQL to Linq 转换器。
  • 有一个名为“linqer”的工具,但由于某种原因我不太喜欢它。可能对你有用...
  • 这不是一条语句,也不是普通的SQL,而是一段程序代码。实际上是 Transact-SQL。这就是为什么 is 不适合单个 LINQ 语句的原因。幸运的是,如下所示,C# 非常适合编写过程代码:-)

标签: sql sql-server linq


【解决方案1】:

由于 LINQ 语法和扩展方法不支持插入,因此无法在 LINQ2SQL 中使用单个语句来完成。以下(假设一个名为 db 的数据上下文)应该可以解决问题。

 if (!db.Users.Any( u => u.UserName == "michael" ))
 {
      db.Users.InsertOnSubmit( new User { UserName = "michael" } );
      db.SubmitChanges();
 }

【讨论】:

  • @Ian - 绝对正确。我不记得我在想什么。
【解决方案2】:

实现tvanfosson方案的扩展方法:

  /// <summary>
  /// Method that provides the T-SQL EXISTS call for any IQueryable (thus extending Linq).
  /// </summary>
  /// <remarks>Returns whether or not the predicate conditions exists at least one time.</remarks>
  public static bool Exists<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
  {
     return source.Where(predicate).Any();
  }

  /// <summary>
  /// Method that provides the T-SQL EXISTS call for any IQueryable (thus extending Linq).
  /// </summary>
  /// <remarks>Returns whether or not the predicate conditions exists at least one time.</remarks>
  public static bool Exists<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
  {
     return source.Where(predicate).Any();
  }

然后将使用扩展方法:

  bool exists = dataContext.Widgets.Exists(a => a.Name == "Premier Widget");

虽然 .Where().Any() 组合足够有效,但它确实有助于代码呈现的逻辑流程。

【讨论】:

    【解决方案3】:

    将 Exists 代码放在静态类中。例如为您的项目添加一个类,例如:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Linq.Expressions;
    
    namespace company.project
    {
       static class LinqExtensions
        {
            /// <summary>
            /// Method that provides the T-SQL EXISTS call for any IQueryable (thus extending Linq).
            /// </summary>
            /// <remarks>Returns whether or not the predicate conditions exists at least one time.</remarks>
            public static bool Exists<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
            {
                return source.Where(predicate).Any();
            }
    
            /// <summary>
            /// Method that provides the T-SQL EXISTS call for any IQueryable (thus extending Linq).
            /// </summary>
            /// <remarks>Returns whether or not the predicate conditions exists at least one time.</remarks>
            public static bool Exists<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate)
            {
                return source.Where(predicate).Any();
            }
    
    
        }
    }
    

    不要忘记将这个类的命名空间添加到使用它的任何其他类中。 ;P

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 2021-02-05
      相关资源
      最近更新 更多