解析字符串并按解析值排序
我正在尝试构建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题更新后 - 这将按
节点值返回您的排序数据:结果有效为
IOrderedEnumerable
类型。After the question update - this will return your ordered data by
<en>
node value:The result valiable is of
IOrderedEnumerable<Test>
type.这将生成
ru
标记中的值列表(假设它们是整数),并按en
标记中的值排序(同样,假设是整数)。如果您只想枚举,可以省略
ToList
调用:This will produce a list of the values in
ru
tags (assuming they are integers), ordered by the values inen
tags (again, assuming integers).If you simply want to enumerate, you can omit the
ToList
call:我不确定我是否得到了例外的结果,但如果您需要选择
en
中的值并按ru
中的值排序,那么这里是: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 inru
then here it is:我不知道是否为时已晚,但如果您想解析文本并且它是整数,则按值排序,否则按文本排序,那么这可能会有所帮助。
您需要定义一个这样的函数来启用 LINQ 表达式中的解析:
然后您可以执行这样的查询:
在您的情况下,您可能需要这样的东西:
这会给您:
如果您想处理非整数(即解析为null) 为零,然后使用以下行更改查询:
然后您会得到:
我希望这会有所帮助。
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:
Then you can do queries like this:
In your case you possibly want something like this:
Which would give you:
If you want to treat non-integers (ie parsed as null) to be zero then change the query by using this line:
Then you'll get this:
I hope this helps.