Linq不工作

时间:2013-11-01 08:36:06

标签: c# mysql linq

我正在使用Linq和MySql数据库。我试图从列表中获取前10个结果,但Linq的Take不会过滤结果,它只返回所有59个结果。

这是代码:

 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;

任何想法都赞赏。

1 个答案:

答案 0 :(得分: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数据库不会过滤它吗?