Skip to content

Instantly share code, notes, and snippets.

@anakahala
Last active December 15, 2015 16:29
Show Gist options
  • Select an option

  • Save anakahala/5289173 to your computer and use it in GitHub Desktop.

Select an option

Save anakahala/5289173 to your computer and use it in GitHub Desktop.
LINQで動的検索(拡張メソッド使用)
/// <summary>
/// Person
/// </summary>
private class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
/// <summary>
/// PersonList
/// </summary>
private List<Person> personList = null;
/// <summary>
/// NameのList取得
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private List<string> GetNameList(string name)
{
var q = from d in this.personList select d;
if (!String.IsNullOrEmpty(name))
{
// パラメータのnameが指定されている場合、含まれているものを取得
q = q.Where(v => v.Name.Contains(name));
}
return q.OrderBy(v => v.Id).Select(v => v.Name).ToList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment