I think here is something wrong with code. I use a class with methods to get tenants from DB:
public List<CrmTenant> GetAllTenants()
{
List<CrmTenant> tenantsList = new List<CrmTenant>();
try
{
var crmTenants =
from tenant in context.CrmTenant
select tenant;
if (crmTenants != null)
{
foreach (var tenant in crmTenants)
{
tenantsList.Add(tenant);
}
}
return tenantsList;
}
catch (Exception ex)
{
this.logger.Write("Failed to get all tenants from database.", "Exceptions", TraceEventType.Error, ex);
return null;
}
}
/// <summary>
/// Gets Tenant by name.
/// </summary>
/// <returns></returns>
public CrmTenant GetTenantByName(string tenantName)
{
CrmTenant crmTenant = null;
try
{
crmTenant =
(from customerCrm in context.CrmTenant
where customerCrm.TenantName == tenantName
select customerCrm).Single();
return crmTenant;
}
catch (Exception ex)
{
this.logger.Write(String.Format("Failed to get tenant '{0}' from database.", tenantName), "Exceptions", TraceEventType.Error, ex);
return null;
}
}
/// <summary>
/// Gets tenant by Username.
/// </summary>
/// <returns></returns>
public CrmTenant GetTenantByUserName(string userName)
{
CrmTenant crmTenant = null;
try
{
crmTenant =
(from customerCrm in context.CrmTenant
where customerCrm.Username == userName
select customerCrm).Single();
return crmTenant;
}
catch (Exception ex)
{
this.logger.Write(String.Format("Failed to get tenant by username '{0}' from database.", userName), "Exceptions", TraceEventType.Error, ex);
return null;
}
}
Here is call for methods
// get tenant
var crmTenant = CrmServiceHandlerFactory.GetInstance().Resolve<TenantManager>().GetTenantByUserName(userName);
I am new in programming, could you please help me to improve this code