Linq to SQL to Linq compiled performance

string id = (from c in context.Users
             where c.u_id == Id
             select c.u_id).SingleOrDefault();

1) How can I improve performance for the above using the Linq query for the above. We are only limited to using .NET 3.5.

2). What will be the efficiency when using the requested linq request for the above code as a percentage?

+2
source share
3 answers

You would create a compiled query via:

Func<YourContextType, int, string> query = CompiledQuery.Compile(
       (YourContextType context, int id) => 
           context.Users.Where(u => u.u_id == id).Select(u => u.u_id)
                  .SingleOrDefault()
       );

Then you would use this as:

string resultId = query(context, Id);

, , . , , . cmopiled- , . , , .

: , , , FirstOrDefault() SingleOrDefault().

+3

:

static readonly Func<ENTITIES, YOUR_ID_TYPE, RETURN_VALUE_TYPE> compiledQuery  =
CompiledQuery.Compile<ENTITIES, YOUR_ID_TYPE, RETURN_VALUE_TYPE>(
(ctx, Id) => (from c in ctx.Users
where c.u_id == Id
select c.u_id).SingleOrDefault();

:

RETURN_VALUE_TYPE results = compiledQuery.Invoke(context, Id);

, , , LINQ . , LINQ LINQ to SQL Entity Framework, .

+1

: LINQ , . , dapper-dot-net:

string id = connection.Query<string>(
    @"select u_id from Users where u_id = @id",
    new { id = Id }).SingleOrDefault();

LINQ, ( ..).

+1

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


All Articles