本文发表在 rolia.net 枫下论坛string filter(string name, int? age, string address, string postalCode, ref int count)
{
string condition = "";
count = 0;
if (name != null)
{
condition = string.Format(" Name='{0}' AND ", name.Replace("'", "''"));
count++;
}
if (age != null)
{
condition += string.Format(" Age={0} AND ", age.Value);
count++;
}
if (address != null)
{
condition += string.Format(" Address='{0}' AND ", address.Replace("'", "''"));
count++;
}
if (postalCode != null)
{
condition += string.Format(" PostalCode='{0}' AND ", postalCode.Replace("'", "''"));
count++;
}
return condition.Trim().TrimEnd("AND".ToCharArray());
}
'And' condition also can be achieved by Sequence Where
IQueryable<TableName> q = DataContex.TableNames;
int count = 0;
if (name != null)
{
q = q.Where(t => t.Name.Equals(name));
}
if (age != null)
{
q = q.Where(t => t.age == age.Value);
}
if (address != null)
{
q = q.Where(t => t.address == address );
}
if (postalcode != null)
{
q = q.Where(t => t.postcode == postalcode);
}更多精彩文章及讨论,请光临枫下论坛 rolia.net