linq 到实体 orderby

发布于 2024-11-09 00:06:44 字数 493 浏览 0 评论 0原文

如何将此查询转换为具有实体框架的实体的 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。有什么想法吗?

How can I convert this query to linq to entities with entity framework:

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

I have this:

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

I don't know how to translate the orderby. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浊酒尽余欢 2024-11-16 00:06:44
return Customers.GetQuery().
            Where(x => x.CompanyId == companyId).
        OrderBy(x=> SqlFunctions.IsNumeric(x.customer_name)).
        ThenBy(x=> x.customer_name);
return Customers.GetQuery().
            Where(x => x.CompanyId == companyId).
        OrderBy(x=> SqlFunctions.IsNumeric(x.customer_name)).
        ThenBy(x=> x.customer_name);
忘东忘西忘不掉你 2024-11-16 00:06:44

您可以在查询中使用 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 };

You can use SqlFunctions.IsNumeric in your query to map to IsNumeric in Sql Server.

Something along the lines of:

        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 };
瞄了个咪的 2024-11-16 00:06:44

首先,我认为 SQL 查询有问题,

WHERE   company_id=@company_id AND

为什么添加 AND

您可以通过使用

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

First I think there something wrong with the SQL query in

WHERE   company_id=@company_id AND

why you added AND ?

you can achieve ISNUMERIC in Entity Framewwork only (not linq to sql) by using SqlFunctions.IsNumeric

return CUstomers.GetQuery().
            Where(x => x.CompanyId == companyId).
            OrderBy(x => SqlFunctions.IsNumeric(x.customer_name)).
            ThenBy(x => x.customer_name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文