订购LINQ声明无效

时间:2011-12-04 12:01:30

标签: linq entity-framework

我无法在LINQ语句中进行一些订购工作,我一直在努力。这是代码:

public static List<Contact> GetAllTheCusts(string fName,
    string lName,
    string middleName,
    int? customerId, 
    string sort, 
    int pageIndex, 
    int pageSize)
{
    AWEntities entities = Common.GetContext();

    int skipCount = pageSize * pageIndex;

    var contacts = entities.Contacts
        .Include("Individuals.Customer")
        .Where(c => customerId.HasValue
                        ? c.Individuals.Any(i => i.CustomerID == customerId.Value)
                        : c.Individuals.Any(i => i.Customer.CustomerID == i.CustomerID))
        .Where(c => string.IsNullOrEmpty(fName) || c.FirstName.Contains(fName))
        .Where(c => string.IsNullOrEmpty(lName) || c.LastName.Contains(lName))
        .Where(c => string.IsNullOrEmpty(middleName) || c.MiddleName.Contains(middleName));
        .Select(c => c);

    IOrderedQueryable<Contact> contactsOrdered = contacts.OrderByDescending(o => o.ContactID);;

    return contactsOrdered.Skip(skipCount).Take(pageSize).ToList();

}

出于某种原因,OrderByDescending方法未执行其角色。请有人帮忙。

干杯

1 个答案:

答案 0 :(得分:0)

以为我会在这里发布最终的工作代码。这已成功将存储过程转换为LINQ到实体:

public static List<Contact> GetCustomersWithContactDetails(string fName, string lName, string middleName, int? customerId, 
        string sort, int pageIndex, int pageSize, out int count)
    {
        AWEntities entities = Common.GetContext();

        int skipCount = pageSize * pageIndex;

        var contacts = entities.Contacts
            .Include("Individuals.Customer")
            .Where(c => customerId.HasValue
                            ? c.Individuals.Any(i => i.CustomerID == customerId.Value)
                            : c.Individuals.Any(i => i.Customer.CustomerID == i.CustomerID))
            .Where(c => string.IsNullOrEmpty(fName) || c.FirstName.Contains(fName))
            .Where(c => string.IsNullOrEmpty(lName) || c.LastName.Contains(lName))
            .Where(c => string.IsNullOrEmpty(middleName) || c.MiddleName.Contains(middleName));

        //  Get ordering based on the "sort" parameter
        var contactsOrdered = GetOrdering(sort, contacts);

        count = contacts.Count();

        return contactsOrdered.Skip(skipCount).Take(pageSize).ToList();
    }

    private static IOrderedQueryable<Contact> GetOrdering(string sort, IQueryable<Contact> contacts)
    {
        IOrderedQueryable<Contact> contactsOrdered;

        if (string.IsNullOrEmpty(sort))
        {
            contactsOrdered = contacts.OrderBy(o => o.LastName);
        }
        else
        {
            SortDirection sortDirection = sort.EndsWith("ASC", StringComparison.Ordinal)
                                              ? SortDirection.asc
                                              : SortDirection.desc;


            switch (sort.Substring(0, sort.IndexOf(' ')))
            {
                case "ContactID":
                    {
                        if (sortDirection == SortDirection.asc)
                        {
                            contactsOrdered = contacts.OrderBy(o => o.ContactID);
                        }
                        else
                        {
                            contactsOrdered = contacts.OrderByDescending(o => o.ContactID);
                        }
                        break;
                    }
                case "LastName":
                    {
                        if (sortDirection == SortDirection.asc)
                        {
                            contactsOrdered = contacts.OrderBy(o => o.LastName);
                        }
                        else
                        {
                            contactsOrdered = contacts.OrderByDescending(o => o.LastName);
                        }
                        break;
                    }
                case "FirstName":
                    {
                        if (sortDirection == SortDirection.asc)
                        {
                            contactsOrdered = contacts.OrderBy(o => o.FirstName);
                        }
                        else
                        {
                            contactsOrdered = contacts.OrderByDescending(o => o.FirstName);
                        }
                        break;
                    }
                case "MiddleName":
                    {
                        if (sortDirection == SortDirection.asc)
                        {
                            contactsOrdered = contacts.OrderBy(o => o.MiddleName);
                        }
                        else
                        {
                            contactsOrdered = contacts.OrderByDescending(o => o.MiddleName);
                        }
                        break;
                    }

                default:
                    {
                        contactsOrdered = contacts.OrderBy(o => o.ContactID);
                        break;
                    }
             }

        }
        return contactsOrdered;
    }

干杯