根据条件更改字段顺序

发布于 2024-11-08 06:36:53 字数 494 浏览 0 评论 0原文

我有一个场景,我必须根据某些条件按字段更改顺序。

from summaryRows in _summaryTable.AsEnumerable()
where summaryRows.Field<string>("AirlineDisplayName")
                 .Equals(airlineName_, StringComparison.OrdinalIgnoreCase) 
orderby summaryRows.Field<decimal>("FareAdult")
select new
{
    summaryTableRow = summaryRows
};

根据条件,我必须将 order by 字段更改为 orderbysummaryRows.Field("BasePricePlusTaxAndFees") 这里,两个字段的数据类型是不同的。我怎样才能在一个查询中完成它?

I have a scenario where I have to change the order by field based on some condition.

from summaryRows in _summaryTable.AsEnumerable()
where summaryRows.Field<string>("AirlineDisplayName")
                 .Equals(airlineName_, StringComparison.OrdinalIgnoreCase) 
orderby summaryRows.Field<decimal>("FareAdult")
select new
{
    summaryTableRow = summaryRows
};

Based on the condition, I have to change the order by field to orderby summaryRows.Field<double>("BasePricePlusTaxAndFees")
Here, both the field data type is different. How can I do it in one query?

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

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

发布评论

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

评论(2

只等公子 2024-11-15 06:36:58

这个怎么样:

orderby conditionIsTrue ? (IComparable)summaryRows.Field<double>("BasePricePlusTaxAndFees") : (IComparable)summaryRows.Field<decimal>("FareAdult")

What about this:

orderby conditionIsTrue ? (IComparable)summaryRows.Field<double>("BasePricePlusTaxAndFees") : (IComparable)summaryRows.Field<decimal>("FareAdult")
合约呢 2024-11-15 06:36:57

我认为使用流畅的 Linq 语法并在构建查询时引入 if 语句,这将是最具可读性的。由于您没有解释您的条件,我假设您有一个名为条件的布尔变量,具有适当的值:

var query = _summaryTable.AsEnumerable()
     .Where(
         summaryRows => summaryRows.Field<string>("AirlineDisplayName")
             .Equals(airlineName_, StringComparison.OrdinalIgnoreCase));

if (condition)
    query = query.OrderBy(summaryRows => summaryRows.Field<decimal>("FareAdult"));
else
    query = query.OrderBy(summaryRows => summaryRows.Field<double>("BasePricePlusTaxAndFees"));

var resultQuery = query.Select(summaryRows => new
    {
        summaryTableRow = summaryRows
    });

免责声明:I还没有测试过,但祝你好运。

I think this will be the most readable using fluent Linq syntax and introducing an if-statement while building the query.. Since you do not explain your condition, I assume that you have a boolean variable called condition with the appropriate value:

var query = _summaryTable.AsEnumerable()
     .Where(
         summaryRows => summaryRows.Field<string>("AirlineDisplayName")
             .Equals(airlineName_, StringComparison.OrdinalIgnoreCase));

if (condition)
    query = query.OrderBy(summaryRows => summaryRows.Field<decimal>("FareAdult"));
else
    query = query.OrderBy(summaryRows => summaryRows.Field<double>("BasePricePlusTaxAndFees"));

var resultQuery = query.Select(summaryRows => new
    {
        summaryTableRow = summaryRows
    });

Disclaimer: I have not tested it, but good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文