如何使用LINQ对数据进行排序 [英] How to Sort Data using LINQ
本文介绍了如何使用LINQ对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我的数据收集是数字和字母的组合时,如何对LINQ的结果进行排序.
下面是我的表的设计和填充行的方法.
Hi,
How do i sort the result of LINQ when my collection of data is combination of numbers and letters.
Below is the design of my tables and my method to populate the line.
Lines
--------------------------------
|LineID | LineName | LineType |
--------------------------------
| 167 | 1 | 3 |
| 172 | 2 | 3 |
| 13 | 3 | 3 |
| 231 | 4 | 3 |
| 18 | 5 | 3 |
| 23 | 6 | 3 |
| 44 | 7 | 3 |
| 49 | 8 | 3 |
| 116 | 9 | 3 |
| 120 | 10 | 3 |
| 266 | 13 | 3 |
| 195 | 11 | 7 |
| 265 | 12 | 7 |
| 2 | B | 1 |
| 3 | C | 1 |
--------------------------------
Line Type
-------------------------
|LineType | LineDesc |
-------------------------
| 3 | FP |
| 7 | BF |
| 1 | Process |
-------------------------
Process
---------------------------------------
|ProcessID | ProcessName| ProcessType |
---------------------------------------
| 10 | Filling | 3 |
| 25 | BulkFilling| 7 |
| 4 | Process | 1 |
---------------------------------------
下面是我在填充Line时使用的方法.它具有参数"processid".
Below is the method that i use when populating the Line. It has a parameter "processid".
private void LoadLinePerProcess(int processid)
{
List<Classes.Lines> line = Classes.Lines.GetLineCollection();
List<Classes.Process> process = Classes.Process.GetProcessCollection();
List<Classes.ProcessLineType> processtype = Classes.ProcessLineType.GetProcessLineTypeCollection();
var qLine = from p in process
join t in processtype on p.ProcessType equals t.LineTypeCode
join l in line on t.LineTypeDescription equals l.LineType into LineColl
orderby p.ProcessType
select new
{
ProcessID = p.ProcessID,
ProcessName = p.ProcessName,
ProcessType = p.ProcessType,
LineName = from ln in LineColl
orderby ln.LineName
select new { ln.LineID, ln.LineName }
};
foreach (var result in qLine)
{
if (result.ProcessID == processid)
{
c_cboLine.Items.Clear();
foreach (var lines in result.LineName)
{
c_cboLine.Items.Add(lines.LineName);
}
break;
}
}
if (c_cboLine.Items.Count > 0) { c_cboLine.SelectedIndex = 0; }
}
我现在的问题是,当我的"processid"等于10时,它将生成的行显示为"1,13,2,3,4,5 ...",看起来它转换为字符串而不是int.
谢谢
My problem now is, when my "processid" is equal to 10, it generate the line as "1,13,2,3,4,5..." it looks like it converted to string not int.
Thanks
推荐答案
orderby p.ProcessType != 1 ? int.Parse(ln.LineName) : char.Parse(ln.LineName)
这解决了我的问题.
谢谢!
this solved my problem.
Thanks!
可以看看 ^ ]链接.
这篇关于如何使用LINQ对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文