2014-10-20 122 views
-2

我想要做一個簡單的方法來返回一個列表。但是會發生錯誤:無法將類型System.Collections.Generic.List<AnonymousType#1>隱式轉換爲System.Collections.Generic.IList<PesquisaView.view_ok>不能隱式地將類型'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的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; 

但在 「返回查詢」

出現錯誤10

感謝

+0

這是因爲您選擇「新建」,請嘗試在選擇新零件中明確指定列表類型。 – User2012384 2014-10-20 01:19:53

+0

你能否舉個例子(或者鏈接到我的undestand)?我在學。 – Elber 2014-10-20 02:06:33

+1

我承認並沒有試圖理解代碼,但我不會在一個linq調用中完成這一切。這太難以理解了。 – 2014-10-20 02:29:17

回答

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; 

    } 

默認情況下,選擇新將選擇匿名類型的枚舉。

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

+0

當我編寫代碼時,錯誤不會發生。但是,當我運行該項目時,運行時錯誤是:EntityFramework.SqlServer.dll 發生類型'System.NotSupportedException'的未處理的異常其他信息:實體或複雜類型'PesquisaBETAModel.view_ok'不能在一個LINQ to Entities查詢。 – Elber 2014-10-20 02:47:19

+0

@Elber是PesquisaBETAModel.view_ok一個複雜的類型? (它是數據庫中的視圖嗎?)還是它是一個類? – User2012384 2014-10-20 02:52:44

+0

是一個自動生成的類實體框架 – Elber 2014-10-20 03:21:03

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; 

    } 
+0

請看看我的解決方案,看看是否有幫助 – User2012384 2014-10-20 02:27:03

0

新的代碼不會出現在代碼中的問題,但是當我運行該項目,實體F.不與錯誤的工作:

的「System.NotSupportedException '發生在EntityFramework.SqlServer.dll其他信息:實體或複雜類型'PesquisaBETAModel.view_ok'不能在LINQ to Entities查詢中構造。

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; 

    } 
相關問題