2013-11-01 46 views
2

我正在使用Linq和MySql數據庫。我試圖從列表中獲得前10個結果,但Linq's Take不會過濾結果,它只返回所有59個結果。Linq不工作

這是代碼:

List<EComQuoteSearchModel> results = new List<EComQuoteSearchModel>(); 

     //do not included quotes marked as deleted in the search results 
     // int deletedStatusId = (int)IMSourcingPortal.Services.ecomQuoteSystem.EComQuoteStatus.Deleted; 
     //&& qo.nquote_status.StatusId != deletedStatusId 

     results = (from qo in db.nquote_orderheaders 
        join st in db.nquote_status on qo.StatusId equals st.StatusId 
        where (qo.QuoteOrderNumber.Contains(term) 
         || qo.nquote_status.Name.Contains(term) 
         || qo.CustomerName.Contains(term) 
         || qo.IMCustomerNumber.Contains(term)) 
        select new EComQuoteSearchModel() 
        { 
         CustomerName = qo.CustomerName, 
         CustomerNo = qo.IMCustomerNumber, 
         QuoteNo = qo.QuoteOrderNumber, 
         Status = st.Name 
        }).ToList(); 

     IEnumerable<EComQuoteSearchModel> firstTen = results.Take(10); 
     var toret = firstTen.ToList(); 
     return toret; 

任何想法理解。

+2

在ToList()之前使用Take() – MikeSW

+0

請向您自己證明您打印出59個文件的代碼。 (另外,在調用'Take'之前,你不應該使用'.ToList',因爲這將迫使你總是從數據庫中下載所有行,而不是讓後端做這件事) –

+0

@MikeSW,我嘗試使用Take ToList(),但結果是一樣的。 –

回答

0

在使用.ToList()實現結果後,它的情況完全相同。如果您只想過濾來自數據庫WITH THE QUERY的前10個結果。在實現查詢之前,您需要包含.Take(10)。

下面的代碼將寫入5行到控制檯。

// This simulates the results from your query after you materialize it with Tolist() 
var results = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; 
foreach(var str in results.Take(5)) 
{ 
    Console.WriteLine(str); 
} 

問題是因爲你的查詢發送到mysql數據庫沒有過濾嗎?