Chris Marcus
Chris Marcus

Reputation: 21

Linq PredicateBuilder not returning results

I'm still ramping up on Linq, but hopefully someone can help me determine why I'm not getting any results from the following method that uses PredicateBuilder (gOrderCount always == 0), any help greatly appreciated:

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();
        }
    }

Upvotes: 0

Views: 1025

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500225

I suspect this is the problem:

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

So you're starting off with a predicate which doesn't match anything, and then adding more restrictions to it, effectively ending up with something like:

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

That's clearly never going to match anything.

You want to use this to start with:

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

so that your query ends up as something like:

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

Upvotes: 3

Dave
Dave

Reputation: 2417

I would break this down a bit before trying to do everything at once. Try this first before creating your TrackingInfo objects.

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;
}

Then you can check the results of the query and tweak your predicates to make sure they are correct.

Upvotes: 0

Related Questions