I have a class say
public class CostStore
{
int DirectorateId { get; set; }
decimal NewCar { get; set; }
decimal ContractorNew { get; set; }
decimal ContractorRenew { get; set; }
decimal ContractorLost { get; set; }
decimal ContractorStolen { get; set; }
decimal InternalNew { get; set; }
decimal InternalRenew { get; set; }
decimal InternalLost { get; set; }
decimal InternalStolen { get; set; }
}
and in my controller i want to find out say
var c = from p in _service.List().Where (condition) p.InternalNew/InternalRenew
(etc) property based on a session variable like so in the function below, how can I do this in linq statement...any ideas (_service.List() lists the IEnumerable of the class CostStore
private string FindProperty()
{
switch (Session[Constants.FORMSESSIONKEY].ToString())
{
case Constants.NEWAPP:
return "InternalNew";
case Constants.LOST:
return "InternalLost";
case Constants.NEWCAR:
return "NewCar";
case Constants.OTHER:
return "InternalStolen";
case Constants.RENEW:
return "InternalRenew";
default:
return String.Empty;
}
}
Currently I am having to do this
private Decimal FindProperty()
{
switch (Session[Constants.FORMSESSIONKEY].ToString())
{
case Constants.NEWAPP:
return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
select p.InternalNew).Single() ?? 0.0M;
case Constants.LOST:
return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
select p.InternalLost).Single();
case Constants.NEWCAR:
return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
select p.NewCar).Single();
case Constants.OTHER:
return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
select p.InternalStolen).Single();
case Constants.RENEW:
return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
select p.InternalRenew).Single();
default:
return 0.0M;
}
}
but its a lot of duplicate code, also no check for sequence containing 0 values...