解析字符串并按解析值排序

发布于 2024-10-20 00:00:25 字数 1135 浏览 1 评论 0原文

我正在尝试构建 linq 表达式来解决我的问题。我有一个字符串列表,

List<string> arr = new List<string>();
arr.Add("<desc><ru>1</ru><en>3</en></desc>");
arr.Add("<desc><ru>2</ru><en>4</en></desc>");

我想解析每个项目和订单结果

fake sample:
arr.Select(ParseItem("en")).OrderBy(x) 

,然后我们在 ru 中有两个项目,顺序为 1,2

感谢大家,抱歉我的英语不好 感谢您的所有回复,但如何将现在的结果转换为 IQueryable

class Test { public string data { get; set; } } 
List<Test> arr = new List<Test>();

arr.Add(new Test { data = "<desc><ru>AAA</ru><en>One</en></desc>" });
arr.Add(new Test { data = "<desc><ru>1</ru><en>Two</en></desc>" });
arr.Add(new Test { data = "<desc><ru>22</ru><en>Ab</en></desc>" });
IQueryable<Test> t = arr.AsQueryable();

// here the trouble how to convert to IQueryable<Test>
t = t.Select(s => XElement.Parse(s.data)).Select(x => x.Element("en")).
OrderBy(el => el.Value);

再次感谢

i am trying to build linq expression to solve my problem. I have list of strings

List<string> arr = new List<string>();
arr.Add("<desc><ru>1</ru><en>3</en></desc>");
arr.Add("<desc><ru>2</ru><en>4</en></desc>");

i want to parse every item and order results

fake sample:
arr.Select(ParseItem("en")).OrderBy(x) 

then we have two items in ru in order 1,2

Thanks for all and sorry for my bad English
Thanks for all response but how to convert now results to IQueryable

class Test { public string data { get; set; } } 
List<Test> arr = new List<Test>();

arr.Add(new Test { data = "<desc><ru>AAA</ru><en>One</en></desc>" });
arr.Add(new Test { data = "<desc><ru>1</ru><en>Two</en></desc>" });
arr.Add(new Test { data = "<desc><ru>22</ru><en>Ab</en></desc>" });
IQueryable<Test> t = arr.AsQueryable();

// here the trouble how to convert to IQueryable<Test>
t = t.Select(s => XElement.Parse(s.data)).Select(x => x.Element("en")).
OrderBy(el => el.Value);

Thanks again

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

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

发布评论

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

评论(4

云淡风轻 2024-10-27 00:00:25

问题更新后 - 这将按 节点值返回您的排序数据:

var result = arr
    .OrderBy(t=>
        XElement.Parse(t.data).Element("en").Value
    );

结果有效为 IOrderedEnumerable 类型。

After the question update - this will return your ordered data by <en> node value:

var result = arr
    .OrderBy(t=>
        XElement.Parse(t.data).Element("en").Value
    );

The result valiable is of IOrderedEnumerable<Test> type.

悸初 2024-10-27 00:00:25

这将生成 ru 标记中的值列表(假设它们是整数),并按 en 标记中的值排序(同样,假设是整数)。

List<string> items = arr.Select(s => XElement.Parse(s))
                        .OrderBy(xml => (int)xml.Element("en"))
                        .Select(xml => (int)xml.Element("ru"))
                        .ToList();

如果您只想枚举,可以省略 ToList 调用:

foreach (var item in arr.Select(s => XElement.Parse(s))
                        .OrderBy(xml => (int)xml.Element("en"))
                        .Select(xml => (int)xml.Element("ru")))
{
    // do something with item
}

This will produce a list of the values in ru tags (assuming they are integers), ordered by the values in en tags (again, assuming integers).

List<string> items = arr.Select(s => XElement.Parse(s))
                        .OrderBy(xml => (int)xml.Element("en"))
                        .Select(xml => (int)xml.Element("ru"))
                        .ToList();

If you simply want to enumerate, you can omit the ToList call:

foreach (var item in arr.Select(s => XElement.Parse(s))
                        .OrderBy(xml => (int)xml.Element("en"))
                        .Select(xml => (int)xml.Element("ru")))
{
    // do something with item
}
终止放荡 2024-10-27 00:00:25

我不确定我是否得到了例外的结果,但如果您需要选择 en 中的值并按 ru 中的值排序,那么这里是:

var orderedItems = (
    from item in arr
    let x = XElement.Parse(item)
    let ruValue = (int)x.Element("ru")
    let enValue = (int)x.Element("en")
    orderby ruValue
    select enValue
    ).ToList();

I'm not sure I've got what the excepted results are, but if you need to select values in en ordered by the value in ru then here it is:

var orderedItems = (
    from item in arr
    let x = XElement.Parse(item)
    let ruValue = (int)x.Element("ru")
    let enValue = (int)x.Element("en")
    orderby ruValue
    select enValue
    ).ToList();
南薇 2024-10-27 00:00:25

我不知道是否为时已晚,但如果您想解析文本并且它是整数,则按值排序,否则按文本排序,那么这可能会有所帮助。

您需要定义一个这样的函数来启用 LINQ 表达式中的解析:

Func<string, int?> tryParseInteger = text =>
{
    int? result = null;
    int parsed;
    if (int.TryParse(text, out parsed))
    {
        result = parsed;
    }
    return result;
};

然后您可以执行这样的查询:

var xs = new [] { "Hello", "3ff", "4.5", "5", };
var rs =
    (from x in xs
     select tryParseInteger(x)).ToArray();
// rs == new int?[] { null, null, null, 5, };

在您的情况下,您可能需要这样的东西:

var elements = new []
{
    "<desc><ru>AAA</ru></desc>",
    "<desc><ru>1</ru></desc>",
    "<desc><ru>42</ru></desc>",
    "<desc><ru>-7</ru></desc>",
    "<desc><ru>BBB</ru></desc>",
    "<desc><ru>22</ru></desc>",
};

var query =
    from e in elements
    let xe = XElement.Parse(e)
    let v = xe.Element("ru").Value
    orderby v
    orderby tryParseInteger(v)
    select v;

这会给您:

{ "AAA", "BBB", "-7", "1", "22", "42" }

如果您想处理非整数(即解析为null) 为零,然后使用以下行更改查询:

    orderby tryParseInteger(v) ?? 0

然后您会得到:

{ "-7", "AAA", "BBB", "1", "22", "42" }

我希望这会有所帮助。

I don't know if it is too late, but if you are wanting to parse the text and if it is an integer then sort by value otherwise sort by text, then this might help.

You need to define a function like this to enable parsing in LINQ expressions:

Func<string, int?> tryParseInteger = text =>
{
    int? result = null;
    int parsed;
    if (int.TryParse(text, out parsed))
    {
        result = parsed;
    }
    return result;
};

Then you can do queries like this:

var xs = new [] { "Hello", "3ff", "4.5", "5", };
var rs =
    (from x in xs
     select tryParseInteger(x)).ToArray();
// rs == new int?[] { null, null, null, 5, };

In your case you possibly want something like this:

var elements = new []
{
    "<desc><ru>AAA</ru></desc>",
    "<desc><ru>1</ru></desc>",
    "<desc><ru>42</ru></desc>",
    "<desc><ru>-7</ru></desc>",
    "<desc><ru>BBB</ru></desc>",
    "<desc><ru>22</ru></desc>",
};

var query =
    from e in elements
    let xe = XElement.Parse(e)
    let v = xe.Element("ru").Value
    orderby v
    orderby tryParseInteger(v)
    select v;

Which would give you:

{ "AAA", "BBB", "-7", "1", "22", "42" }

If you want to treat non-integers (ie parsed as null) to be zero then change the query by using this line:

    orderby tryParseInteger(v) ?? 0

Then you'll get this:

{ "-7", "AAA", "BBB", "1", "22", "42" }

I hope this helps.

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