linq到实体orderby

时间:2011-05-24 06:59:23

标签: c# .net entity-framework linq-to-entities

如何将此查询转换为linq到具有实体框架的实体:

SELECT  customer_id,
    customer_name,
    customer_code
FROM dbo.V_STF_CUSTOMER
WHERE   company_id=@company_id AND
ORDER BY    CASE
            WHEN ISNUMERIC(customer_name)=1 THEN 1
            ELSE 0
        END,
        customer_name

我有这个:

return CUstomers.GetQuery().
            Where(x => x.CompanyId == companyId).
        OrderBy(??This is my problem??);

我不知道如何翻译orderby。有什么想法吗?

3 个答案:

答案 0 :(得分:8)

return Customers.GetQuery().
            Where(x => x.CompanyId == companyId).
        OrderBy(x=> SqlFunctions.IsNumeric(x.customer_name)).
        ThenBy(x=> x.customer_name);

答案 1 :(得分:1)

您可以在查询中使用SqlFunctions.IsNumeric映射到Sql Server中的IsNumeric。

有些事情:

        var results = from c in customers
                      where c.companyId = companyId
                      orderby SqlFunctions.IsNumeric(c.customerName) == 1 ? 1 : 0, c.customerName
                      select new { c.customerId, c.customerName, c.customerCode };

答案 2 :(得分:0)

首先我认为

中的SQL查询有问题
WHERE   company_id=@company_id AND

为什么添加AND

您只能使用SqlFunctions.IsNumeric

在Entity Framewwork中实现ISNUMERIC(而不是linq to sql)
return CUstomers.GetQuery().
            Where(x => x.CompanyId == companyId).
            OrderBy(x => SqlFunctions.IsNumeric(x.customer_name)).
            ThenBy(x => x.customer_name);