Best way for retrieving single record results in LINQ to SQL

后端 未结 6 1436
清酒与你
清酒与你 2021-02-01 13:09

If I query a table with a condition on the key field as in:

        var user = from u in dc.Users
                   where u.UserName == usn
                   s         


        
6条回答
  •  没有蜡笔的小新
    2021-02-01 13:57

    Try something like this:

    var user = (from u in dc.Users
                       where u.UserName == usn
                       select u).FirstOrDefault();
    

    The FirstOrDefault method returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.

提交回复
热议问题