Working with a database TABLE called Costcenter.
I am trying to show the Costcenters in a dropdownlist where CostcenrCode is only three numeric characters. I was trying to do that on the VIEW on an ASP.Net WF application. Now I have moved it to the DataAccess query where I am using LINQ to SQL. I am bit confused, how to do it in the LINQ query where I am returning String. I am selecting cc at the end which is populating all the Costcenters in the database table. But I need to only pull the one is like (e.g. 100 instead of F.C56).
My DataAccess code is as follows:
public static IEnumerable<Costcenter> GetAllCostcentersByCountryCompanyProfitcenterYear(short year, int companyID)
{
List<Costcenter> costcenters = new List<Costcenter>();
using (var context = new CostReportEntities())
{
costcenters = (from cc in context.Costcenters
join company in context.Companies on cc.CompanyID equals company.CompanyID
where cc.Year == year && cc.CompanyID == companyID
select cc).ToList();
}
return costcenters;
}
I have been looking at few posts here, but couldn’t put anything together since I am new in LINQ to SQL.
This may do the trick:
Source:
LINQ to SQL query to determine if value starts with numeric
Edit 1 – added alternative solution using
SqlMethods.PatIndex: