Waller
Waller

Reputation: 476

Is there a simple way to get a specific column data from the db

I have a list data like this

var empData = this._db.empObj
                      .Where(x => x.Id == 18)
                      .OrderByDescending(x => x.Name)
                      .ToList();

Is there a way I can get a specific column data from the db as simple as the above? Something like:

string Name = this._db.empObj.Where(x => x.Id == 18)

Upvotes: 0

Views: 46

Answers (1)

MichaelMao
MichaelMao

Reputation: 2796

You can try Single or SingleOrDefault

var name = this._db.empObj.SingleOrDefault(x => x.Id == 18)?.Name;
var name = this._db.empObj.Single(x => x.Id == 18).Name;

If you want to get a list of name

var names = this._db.empObj.Where(x => x.Id == 18).Select(x => x.Name);

Upvotes: 2

Related Questions