Linq PredicateBuilder没有返回结果

时间:2011-05-27 19:56:58

标签: linq silverlight-4.0 predicatebuilder

我仍在使用Linq,但希望有人可以帮我确定为什么我没有从以下使用PredicateBuilder的方法得到任何结果(gOrderCount总是== 0),任何帮助都非常感激:

public IQueryable<TrackingInfo> GetTrackingAllOrders(string custName, string supplier, string assigned)
    {
        var predicate = PredicateBuilder.False<TrackingInfo>();

        if (custName != null && custName != String.Empty)
        {
            predicate = predicate.And(c => c.CustomerName.Contains(custName));
        }
        if (supplier != null && supplier != String.Empty)
        {
            predicate = predicate.And(c => c.Supplier.Contains(supplier));
        }
        if (assigned != null && assigned != String.Empty)
        {
            predicate = predicate.And(c => c.AssignedTo.Contains(assigned));
        }

         IQueryable<TrackingInfo> oList = null;

        using (var ctx = new OMS_ISSEntities())
        {
            oList = (from c in ctx.GetSSISTrackingInfoFuction()
                     orderby c.OrderID descending
                     select new TrackingInfo
                     {
                         OrderID = c.OrderID,
                         CustomerName = c.CustomerName ?? String.Empty,
                         ClientServiceID = c.ClientServiceID ?? String.Empty,
                         SiteName = c.SiteName ?? String.Empty,
                         SiteStatus = c.SiteStatus ?? String.Empty,
                         IPNETWCount = c.IPNETWCount ?? 0,
                         VOIPCount = c.VOIPCount ?? 0,
                         AirCardCount = c.AirCardCount ?? 0,
                         TicketProductType = c.TicketProductType ?? String.Empty,
                         SiteAddress = c.SiteAddress ?? String.Empty,
                         LCONPhone = c.LCONPhone ?? String.Empty,
                         Supplier = c.Supplier ?? String.Empty,
                         SupplierOrderNumber = c.SupplierOrderNumber ?? String.Empty,
                         ConfFOCDate = c.ConfFOCDate,
                         DSLNumber = c.DSLNumber ?? String.Empty,
                         DSLLineType = c.DSLLineType ?? String.Empty,
                         JournalNote = c.JournalNote ?? String.Empty,
                         JournalLastUpdate = c.JournalLastUpdate,
                         Project = c.Project ?? String.Empty,
                         SiteICB = c.SiteICB,
                         SiteISSDueDate = c.SiteISSDueDate,
                         SiteISSInfo = c.SiteISSInfo ?? String.Empty,
                         AssignedTo = c.AssignedTo ?? String.Empty,
                         SiteSubmitDate = c.SiteSubmitDate,
                         SiteID = c.SiteID ?? String.Empty,
                         UserLogin = c.UserLogin ?? String.Empty,
                         ClientSiteType = c.ClientSiteType ?? String.Empty,
                         OpenJeop_Supp = c.OpenJeop_Supp,
                         PastDueFOC = c.PastDueFOC,
                         DaysSinceLastJNUpdate = c.DaysSinceLastJNUpdate,
                         SiteStatusID = c.SiteStatusID,
                         AssignedToID = c.AssignedToID,
                         SupplierID = c.SupplierID,
                         MasterCustID = c.MasterCustID,
                         MaxJeopSuppDate = c.MaxJeopSuppDate,
                         EstimatedTTU = c.Sit_EstTTU
                     }).ToList().AsQueryable().Where(predicate);

            gOrderCount = oList.Count();


            if (gOrderCount > 1500)  //we limit max number of records returned to 1500
            {
                return null;
            }

            return oList.AsQueryable();
        }
    }

2 个答案:

答案 0 :(得分:3)

我怀疑这是问题所在:

var predicate = PredicateBuilder.False<TrackingInfo>();

所以你开始使用与任何不匹配的谓词,然后添加更多限制,有效地结束如下:

var results = entities.Where(c => false && c.CustomerName.Contains("fred"));

显然永远不会匹配任何东西。

您希望以此开头:

var predicate = PredicateBuilder.True<TrackingInfo>();

以便您的查询最终结果如下:

var results = entities.Where(c => true && c.CustomerName.Contains("fred"));

答案 1 :(得分:0)

在尝试一次完成所有事情之前,我会稍微分解一下。在创建TrackingInfo对象之前先尝试此操作。

using (var ctx = new OMS_ISSEntities())
{
    var oList = from c in ctx.GetSSISTrackingInfoFuction().AsExpandable()
                orderby c.OrderID descending
                where c.Any(Predicate.Compile())
                select c;
}

然后,您可以检查查询结果并调整谓词以确保它们是正确的。