与Linq Dynamic合作

时间:2015-11-19 14:22:37

标签: c# linq

我想将一个组创建为从下拉列表中选择的值,但该值可以是DateTime或String,并且我只想提供一个案例。你有任何想法吗?

我的代码:

public class Personne
{
    public String _NOM { get; set; }
    public DateTime _DDN { get; set; }
    public String _VILLE { get; set; }

    public Personne(String nom, DateTime ddn, String ville)
    {
        this._NOM = nom;
        this._DDN = ddn;
        this._VILLE = ville;
    }
}

这是buttonClick事件:

List<Personne> _liste = new List<Personne>();
_liste.Add(new Personne("toto", new DateTime(1993, 3, 23), "Paris"));
_liste.Add(new Personne("tata", new DateTime(1993, 3, 23), "New-York"));
_liste.Add(new Personne("titi", new DateTime(1987, 2, 10), "Paris"));

var qry = _liste.AsQueryable().OrderBy(listeCritere.SelectedValue)
    .GroupBy(listeCritere.SelectedValue, "It", null);

if (listeCritere.SelectedValue == "_DDN")
{
    foreach (IGrouping<DateTime, Personne> item in qry)
    {
        Response.Write(item.Key + "<br/>");

        foreach (Personne p in item)
        {
            Response.Write("&nbsp;&nbsp;&nbsp;&nbsp; Nom complet : " + p._NOM + "<br/>");
        }
    }
}
else
{
    foreach (IGrouping<String, Personne> item in qry)
    {
        Response.Write(item.Key + "<br/>");

        foreach (Personne p in item)
        {
            Response.Write("&nbsp;&nbsp;&nbsp;&nbsp; Nom complet : " + p._NOM + "<br/>");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以循环遍历IEnumerable<Personne>,然后在循环内获取密钥的正确类型:

var qry = _liste.AsQueryable().OrderBy(listeCritere.SelectedValue)
    .GroupBy(listeCritere.SelectedValue, "It", null);

foreach (IEnumerable<Personne> item in qry)
{
    if (item is IGrouping<DateTime, Personne>)
        Response.Write(((IGrouping<DateTime, Personne>)item).Key + "<br/>");
    else if (item is IGrouping<object, Personne>)
        Response.Write(((IGrouping<object, Personne>)item).Key + "<br/>");

    foreach (Personne p in item)
    {
        Response.Write("&nbsp;&nbsp;&nbsp;&nbsp; Nom complet : " + p._NOM + "<br/>");
    }
}

您还可以将循环移动到通用函数:

public void ButtonClickHandler(object sender, EventArgs e)
{
    var qry = _liste.AsQueryable().OrderBy(listeCritere.SelectedValue)
        .GroupBy(listeCritere.SelectedValue, "It", null);

    if (listeCritere.SelectedValue == "_DDN")
        WritePersonneResponse<DateTime>(Response, qry);
    else
        WritePersonneResponse<string>(Response, qry);
}

public void WritePersonneResponse<T>(HttpResponse response, IQueryable qry)
{
    foreach (IGrouping<T, Personne> item in qry)
    {
        response.Write(item.Key + "<br/>");

        foreach (Personne p in item)
        {
            response.Write("&nbsp;&nbsp;&nbsp;&nbsp; Nom complet : " + p._NOM + "<br/>");
        }
    }
}

答案 1 :(得分:1)

在linq中你可以在闭包中创建函数 - 这里我们只是创建一个函数来返回键值(但总是作为字符串):

string gby = "NOM";
//string gby = "DDN";
//string gby = "VILLE";

var qry = _liste
             .GroupBy(x => {
                string result = "";
                switch (gby) 

                {
                  case "NOM": result = x._NOM; break;
                  case "DDN" : result = x._DDN.ToString(); break;
                  case "VILLE" : result = x._VILLE ; break;
                }
                return result;
            }
            ).ToList();