Consider the following examples, in all of which, I am trying to get the code and the name of a province/state having a known ID, into a single string variable, with a format like "CA-California":
string stateName = _repository.States.Single(s => s.StateId == stateId).Name;
string stateCode = _repository.States.Single(s => s.StateId == stateId).Code;
string stateCodeName = stateCode + "-" + stateName;
var state = _repository.States.Single(s => s.StateId == stateId);
string stateCodeName = state.Code + "-" + state.Name;
string stateCodeName = _repository.States.Where(s => s.StateId == stateId)
.Select(s => s.Code + "-" + s.Name)
.First();
In the first example, only the needed properties of the state are retrieved, but at the cost of running two different queries. The second example runs one query only, but it retrieves all of the properties of the state. The third example seems to be all right, but my question is if there is a way to use Single and First methods to retrieve an arbitrary number of properties from an object (or columns from a table), as opposed to getting them all or only one at a time?
Thank You