Calculating LINQ and .COUNT Times

I have a general question and curiosity regarding LINQ and timeouts.

I have a working application, and I get timeouts for the following code.

The following code, which is normal, and I see nothing wrong:

private static tblUser GetUserLinq (string email, string password) {DataContext db = new DataContext ();

        var tblUsers = from user in db.tblUsers
                           where user.EmailAddress == email
                           && user.Password == password
                           select user;

        if (tblUsers.Count() == 0)
            return null;

        return tblUsers.First();
    }

But getting the following timeouts, a lot, on:

        if (tblUsers.Count() == 0)

Here is the exception

. . - System.Data.SqlClient.SqlException: . .   System.Data.SqlClient.SqlConnection.OnError( SqlException, Boolean breakConnection)   System.Data.SqlClient.SqlInternalConnection.OnError( SqlException, breakConnection)   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)   System.Data.SqlClient.SqlDataReader.ConsumeMetaData()   System.Data.SqlClient.SqlDataReader.get_MetaData()   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String, DbAsyncResult)   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String)   System.Data.SqlClient.SqlCommand.ExecuteReader( CommandBehavior, String)   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader( CommandBehavior)   System.Data.Common.DbCommand.ExecuteReader()   System.Data.Linq.SqlClient.SqlProvider.Execute( , QueryInfo queryInfo, IObjectReaderFactory factory, Object [] parentArgs, Object [] userArgs, ICompiledSubQuery [] subQueries, Object lastResult)   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll( , QueryInfo [] queryInfos, IObjectReaderFactory factory, [] userArguments, ICompiledSubQuery [] subQueries)   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute( )   System.Data.Linq.DataQuery 1.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Linq.Queryable.Count[TSource](IQueryable 1 )   Actions.GetUserLinq(String email, String password) C:\Actions.cs: 104   Login (String email, String password) C:\Actions.cs: 33

, :

, , , , :

        var tblUsers = (from user in db.tblUsers
                           where user.EmailAddress == email
                           && user.Password == password
                           select user).FirstOrDefault(u => u.UserId <0) ;

, LINQ

, , , SQL SQL Enterprise Manager, , , , , , , , LINQ

count (userid) tbluser

+3
4

. , / .

+3

SQL Enterprise Manager, , .

LINQ - , , . - .

, , :

private static tblUser GetUserLinq(string email, string password) 
{ 
    DataContext db = new DataContext();

    var tblUsers = from user in db.tblUsers
                       where user.EmailAddress == email
                       && user.Password == password
                       select user;

    return tblUsers.FirstOrDefault();
}

( "u = > u.UserId < 0" .)

FirstOrDefault null, , .

, . EmailAddress Password?

+3

, :

  if (tblUsers.Count() == 0)
            return null;

, Any(), SQL :

if ( !tblUsers.Any())
    return null;
+3

, , . " ", , .

The point may be that after the request time has elapsed, you will not receive a fulfillment plan, so you will have to use the "Assessment Implementation Plan" function. (The icons for them look like little blue and green rectangles connected together)

+1
source

Source: https://habr.com/ru/post/1705954/


All Articles