2012-03-17 61 views
1

我試圖返回列表清單。任何建議都是非常可觀的。提前致謝。返回使用實體框架

它拋出這個錯誤:

Cannot implicitly convert type System.Collections.Generic.List<HTentityFramework.tblFlower> to
System.Collections.Generic.List<HTentityFramework.testDomain>

代碼是:

public class GetFlowers 
{ 
    public IList<testDomain> getFlowerList() 
    { 
     TestContainer ctx = new TestContainer(); 
     return ctx.tblFlowers.ToList(); 
    } 
} 

public class testDomain 
{ 
    public string Name { get; set; } 
    public int quantity { get; set; } 
} 
+0

你有問題嗎? – SLaks 2012-03-17 01:35:27

+0

它將錯誤引發爲錯誤\t無法將類型'System.Collections.Generic.List '隱式轉換爲'System.Collections.Generic.List ' – Kurkula 2012-03-17 02:35:47

回答

4

錯誤是相當明確的 - 只是閱讀!

您從getFlowerList返回的類型是IList<testDomain> - 但您是從EF對象集中選擇的,該對象集的名稱爲tblFlowers

這個錯誤清楚地說,這是一個IList<tblFlower> - tblFlower對象的列表。

您的代碼不能將tblFlowers列表轉換爲testDomain列表 - 這就是整個問題。

你需要或者自己提供一個轉換,或者你需要從方法返回一個IList<tblFlower>

public IList<tblFlower> getFlowerList() <==== return an IList<tblFlower> here!! 
{ 
    TestContainer ctx = new TestContainer(); 
    return ctx.tblFlowers.ToList(); 
} 
+0

感謝您解決此問題。但是,我們不使用TESTDOMAIN班ILIST? – Kurkula 2012-03-17 16:16:47

+0

@Usham:如果從'tblFlowers'選擇,然後你回來'tblFlower'對象的列表 - 所以你需要返回那些.....如果你想「隱藏」這一事實,並返回一些其他類型的,而不是 - 這是**給你**提供某種由EF('tblFlower')一些**其他數據類型**,你可能已經定義回到現實,實際類型轉換/映射。還是那句話:你的工作,它需要清晰,明確的 - 在.NET中沒有魔法可以爲你做這個 – 2012-03-17 16:18:26