【问题标题】:Pass linq anonymous object to function将 linq 匿名对象传递给函数
【发布时间】:2015-01-09 16:06:00
【问题描述】:

我的控制器上有这个 Linq 查询,它将给我一个 List 中的对象,我需要将此对象传递给另一个类中的函数,该函数将使用它。

这是我的 Linq 查询:

var cursos = (from c in db.Curso
              join cc in db.CursoCategoria on c.cursoCategoriaId equals cc.ID
              where c.ativo == 1 && c.destaque == 1
              select new { ID        = c.ID,
                           nome      = c.nome,
                           imgAbre   = c.imagemAbre,
                           imgFecha  = c.imagemEfeito,
                           cURL      = c.URL,
                           ccURL     = cc.URL,
                           cCor      = cc.cor}).Take(10).ToList();

这就是我调用函数的方式:

var objHelper = new HelperController();
siteVM.lstCursosWall = Json(objHelper.MontaWallCurso());

这是我在HelperController中的函数,这个类也是一个Controller,我有它是因为我有几个帮助函数和项目的ajax方法:

public string MontaWallCurso()
{
     //TODO STUFF       
}

那么,如果我没有类型,我该如何传递这个对象呢?

注意:我使用的是我使用 EF 从我的数据库创建的 DAL 模型,不知道这是否有用。

编辑

所以我改变了我的方法并创建了一个类来填充和传输数据,但现在我在将 linq 结果传递给对象时遇到了问题。

var siteVM  = new SiteViewModel();
var cursos  = new List<CursoWall>();
var lCursos = (from c in db.Curso
               join cc in db.CursoCategoria on c.cursoCategoriaId equals cc.ID
               where c.ativo == 1 && c.destaque == 1
               select new { ID        = c.ID,
                            nome      = c.nome,
                            imgAbre   = c.imagemAbre,
                            imgFecha  = c.imagemEfeito,
                            cURL      = c.URL,
                            ccURL     = cc.URL,
                            cCor      = cc.cor}).Take(10).ToList();

cursos.AddRange(lCursos);

班级:

public class CursoWall
{
    public int ID { get; set; }
    public string nome { get; set; }
    public string imgAbre { get; set; }
    public string imgFecha { get; set; }
    public string cURL { get; set; }
    public string ccURL { get; set; }
    public string cCor { get; set; }
}

【问题讨论】:

  • 代替select new {...select new CursoWall() {...
  • 您也可以摆脱 lCursos 变量和 AddRange 并直接分配,因为您正在创建一个新 List
  • 完美运行,感谢@Hogan。

标签: c# .net asp.net-mvc linq asp.net-mvc-3


【解决方案1】:

不能。在这种情况下,您需要为 DTO 定义一个简单的“Poco”类型进行数据传输。

http://rlacovara.blogspot.nl/2009/03/what-is-difference-between-dto-and-poco.html

【讨论】:

  • 好的,我创建了他提到的类,但现在我找不到将 linq 传递给它的方法,我收到转换问题错误!它在编辑部分
  • @Terkhos - 查看我对主要问题的评论
【解决方案2】:

如果你愿意牺牲类型安全,你可以使用 C# 的动态支持。

public string MontaWallCurso(IList<dynamic> cursos) {
    // use dynamic properties or something that takes dynamic arguments
    foreach( dynamic curso in cursos ) {
        curso.SomePropertyYouAreSureExists = 1;
        curso.SomeMethodYouAreSureExists();
    }
}

或者,如果您愿意牺牲类型安全并处理麻烦,您可以使用反射。在某些情况下,您可以间接执行此操作,例如JSON 转换器或其他依赖反射的库代码。

public string MontaWallCurso(IList<object> cursos) {
    // use reflection or something that uses reflection itself
    foreach( object curso in cursos ) {
        Type cursoType = curso.GetType();
        cursoType.GetProperty("SomePropertyYouAreSureExists").SetValue( curso, 1 );
        cursoType.GetProperty("SomeMethodYouAreSureExists").Invoke( curso, null );
    }
}

为了澄清,在这些方法中,我可能会推荐的唯一一种是基于库的反射,如果它适用的话。

【讨论】:

  • 调用函数时出错:cannot convert from 'System.Collections.Generic.List&lt;AnonymousType#1&gt;' to 'System.Collections.Generic.IList&lt;dynamic&gt;
  • 我很抱歉;我只测试了数组参数。使用 ToArray() 而不是 ToList() 可以避免您遇到的问题。
  • 我之前的评论是指您的代码的预编辑版本。
  • 没问题,但我需要它是一个列表,因为我是如何使用它的,但无论如何谢谢。
猜你喜欢
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多