I have one table containing some fields like Firstname,companyname etc.. Now I want the Companyname whose First Character is starting from “A” and if Company name is empty then search condition will be applied on First name i.e First Character starts from “A”.how can i solve it? Give me query for that. I have applied query like :
var query = _affiliateRepository.Table;
if (!showHidden)
query = query.Where(a => a.Active);
if (Where(from a in query where a.Address.Company.HasValue))
query = from a in query where a.Address.FirstName.StartsWith("a") select a;
else
query = from a in query where a.Address.Company.StartsWith("a") select a;
query = from a in query where a.Address.Company.StartsWith("a") select a;
If I understand your question, I think you need the LINQ equivalent of a COALESCE in SQL. For each row you need to look at both the Company Name and the FirstName to determine which to do the comparison against.
Something like this may work:
.Select (c => (c.Company ?? c.FirstName))
.Where (c => c.StartsWith (“A”))