简体   繁体   English

如何将LINQ与dynamic一起使用? 泛型?

[英]How can I use LINQ with dynamic? Generics?

I have this piece of code: 我有这段代码:

public void RepeaterListato_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
    if (listType == "pages")
    {
        var item = (Pagina)e.Item.DataItem;
        BuildFoto(e, item, IDCategoriaImmaginiPacchettoOfferta);
    }
    else if (listType == "schede")
    {
        var item = (Scheda)e.Item.DataItem;
        BuildFoto(e, item, IDCategoriaImmaginiPacchettoOfferta);
    }
    else if (listType == "news")
    {
        var item = (New)e.Item.DataItem;
        BuildFoto(e, item, IDCategoriaImmaginiPacchettoOfferta);
    }
}

private void BuildFoto(RepeaterItemEventArgs e, dynamic item, string id)
{
    var immagine = item.Immagini.Cast<Allegato>().Where(p => p.Categoria == id).FirstOrDefault();
    if (immagine != null)
    {
        // ...
    }
}

so, due to the type of listType (resolved at Page_Load ), the item changes, so I use dynamic . 因此,由于listType的类型(在Page_Load解析), item发生了变化,因此我使用dynamic But LINQ doesn't works with Cast and Where on dynamically dispatched opertions. 但是LINQ不适用于CastWhere上的动态调度操作。

Is there a workaround? 有解决方法吗? Should I use Generics in your opinions? 我应该在您的意见中使用Generics吗? Best approches? 最好的方法?

Since you don't have access to the source code of Pagina, Scheda and New, you don't have many options 由于您无权访问Pagina,Scheda和New的源代码,因此您没有太多选择

One is to: 一种是:

public void RepeaterListato_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
    IEnumerable<Immagini> immagini = null;

    switch (listType)
    {
      case "pages":
        immagini = ((Pagina)e.Item.DataItem).Immagini;
        break;

      case "schede":
        immagini = ((Scheda)e.Item.DataItem).Immagini;
        break;

      case "news":
        immagini = ((New)e.Item.DataItem).Immagini;
        break;
    }

    if (immagini != null)
    {
      BuildFoto(e, immagini, IDCategoriaImmaginiPacchettoOfferta);
    }
}

private void BuildFoto(RepeaterItemEventArgs e, IEnumerable<Immagini> immagini, string id)
{
    var immagine = immagini.Cast<Allegato>().Where(p => p.Categoria == id).FirstOrDefault();
    if (immagine != null)
    {
        // ...
    }
}

Another option is to use reflection to get the immagini collection instance 另一个选择是使用反射获取immagini集合实例

And yet another option is to create wrapper classes: 还有另一个选择是创建包装器类:

public interface IImmaginiContainer
{
  IEnumerable<IImmagine> Immagini { get; }
}

public class NewWrapper : IImmaginiContainer
{
  public NewWrapper(New source)
  {
    _source = source;
  }
  private readonly New _source;

  public IEnumerable<IImmagine> Immagini => _source.Immagini;
}

// Create a similar class for Scheda and Pagina

private void BuildFoto(RepeaterItemEventArgs e, IImaginiContainer item, string id)
{
    var immagine = item.Immagini.Cast<Allegato>().Where(p => p.Categoria == id).FirstOrDefault();
    if (immagine != null)
    {
        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM