`from..where` or `FirstOrDefault` in LINQ

前端 未结 3 1603
滥情空心
滥情空心 2021-01-30 20:36

Traditionally, when I\'ve tried to get data for a user from a database, and I\'ve used the following method (to some degree):

DbUsers curUser = context.DbUsers.F         


        
3条回答
  •  庸人自扰
    2021-01-30 21:10

    The second is better. You only get the needed data from database so the network traffic is lighter.

    You can have the same result with extension methods:

    var user = context.DbUsers
                      .Where(x => x.u_LoginName == id)
                      .Select(x => new {...})
                      .FirstOrDefault();
    

提交回复
热议问题