无法隐式转换类型&#39; System.Collections.Generic.List <anonymoustype#1>&#39; to&#39; System.Collections.Generic.IList <pesquisaview.view_ok>&#39; </pesquisaview.view_ok> </anonymoustype#1>

时间:2014-10-20 01:17:32

标签: c# linq

我正在尝试使用一种简单的方法来返回列表。但是发生错误:无法隐式将System.Collections.Generic.List<AnonymousType#1>类型转换为System.Collections.Generic.IList<PesquisaView.view_ok>

我尝试了很多东西,但是我找不到带参数和LINQ的例子来解决这个问题。

任何帮助?

CODE:

public class LinqQuery
{
    public IList<view_ok> GetPesquisar(string r8, string r9, string r10, string r11, string sexo, string prof)
    {

        string rs8 = r8;
        string rs9 = r9;
        string rs10 = r10;
        string rs11 = r11;
        string sex = sexo;
        string pr = prof;

        PesquisaBETAEntities pb = new PesquisaBETAEntities();

    var query =  
`   (from t0 in (
    (from v0 in pb.view_ok
    group v0 by new {
        v0.prof_nome,
        v0.resu1
    } into g
    select new {
        g.Key.prof_nome,
        g.Key.resu1}))
    join t1 in (
    from v1 in pb.view_ok
        where Convert.ToString(v1.resu8).Contains(rs8) && Convert.ToString(v1.resu9).Contains(rs9) 
        && Convert.ToString(v1.resu10).Contains(rs10) && Convert.ToString(v1.resu11).Contains(rs11)   && Convert.ToString(v1.sexo).Contains(sex)
    group v1 by new {
      v1.prof_nome,
      v1.resu1
    } into g
    select new {
      g.Key.prof_nome,
      g.Key.resu1,
      perguntas = g.Count(p => p.resu1 != null)
    })
      on new { t0.prof_nome, Resu1 = t0.resu1 }
     equals new { t1.prof_nome, Resu1 = t1.resu1 } into t1_join
    from t1 in t1_join.DefaultIfEmpty()
`

..............

重复Linq for 7连接,最后是:

.............

select new {
t0.prof_nome,
resultado = Convert.ToString(t0.resu1) == "60" ? "Regular" : Convert.ToString(t0.resu1) == "80" ?     "Bom" : 
Convert.ToString(t0.resu1) == "90" ? "Ótimo" : Convert.ToString(t0.resu1) == "100" ? "Excelente" : null,
p1 = (int?)t1.perguntas,
p2 = (int?)t2.perguntas,
p3 = (int?)t3.perguntas,
p4 = (int?)t4.perguntas,
p5 = (int?)t5.perguntas,
p6 = (int?)t6.perguntas,
p7 = (int?)t7.perguntas
}).ToList();

        return query;

但错误发生在“返回查询”

由于

3 个答案:

答案 0 :(得分:0)

我做得更简单,但错误以同样的方式发生:

    public List<view_ok> ListarPesquisa(string prof)
    {
        PesquisaBETAEntities pb = new PesquisaBETAEntities();

        var query = (from t0 in
                         (from v0 in pb.view_ok
                          group v0 by new
                          {
                              v0.prof_nome,
                              v0.resu1
                          }
                              into gg
                              select new
                              {
                                  gg.Key.prof_nome,
                                  gg.Key.resu1
                              })
                       where t0.prof_nome.Contains(prof)
                     select new { Professor = t0.prof_nome, P1 = t0.resu1 }).ToList();

        return query;

    }

答案 1 :(得分:0)

public List<view_ok> ListarPesquisa(string prof)
    {
        PesquisaBETAEntities pb = new PesquisaBETAEntities();

        var query = (from t0 in
                         (from v0 in pb.view_ok
                          group v0 by new
                          {
                              v0.prof_nome,
                              v0.resu1
                          }
                              into gg
                              select new
                              {
                                  gg.Key.prof_nome,
                                  gg.Key.resu1
                              })
                       where t0.prof_nome.Contains(prof)
                     select new view_ok{ Professor = t0.prof_nome, P1 = t0.resu1 }).ToList();

        return query;

    }

默认情况下,select new将选择一个匿名类型的可枚举。

参考: http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/01/25/using-your-own-defined-type-in-a-linq-query-expression.aspx

答案 2 :(得分:0)

新代码在代码中没有出现问题,但是当我运行项目时,实体F不能处理错误:

EntityFramework.SqlServer.dll中发生'System.NotSupportedException'附加信息:无法在LINQ to Entities查询中构造实体或复杂类型'PesquisaBETAModel.view_ok'。

    public List<view_ok> ListarPesquisa(string prof)
    {
        PesquisaBETAEntities pb = new PesquisaBETAEntities();

        var query = (from t0 in pb.view_ok
                     where t0.prof_nome.Contains(prof)
                     select new view_ok { prof_nome = t0.prof_nome, resu1 = t0.resu1 }).ToList();
        return query;

    }
相关问题