How to return single column from database using LINQ

public string GetCustomerName(int CustomerId)
 {
    var results = (from var in CustomerNames
                  where Var.CustomerId == CustomerId)
                  select Var.FirstName);
results.FirstOrDefault();
 }


Other possible variants:

.First() returns the first item and throws an exception if no item exists. It ignores all other items.

.FirstOrDefault() returns the first item if exists and returns the default value of the result type if nothing exists. It ignores all other items.

.Single() returns the only item if the collection contains exactly one item and throws an exception otherwise.

.SingleOrDefault() is like .Single() but returns the default value of the result type instead of throwing an exception.
Ebook Download
View all
Learn
View all