[英]LINQ query on a list of Object[] arrays
I have a list of Object arrays (List) That I am going to pass into a DLL function to apply PDF controls. 我有一个Object数组列表(List)我将传递给DLL函数来应用PDF控件。 The first index of each object is supposed to identify what the control will be.
每个对象的第一个索引应该用于标识控件的内容。 I was wondering if there is some way to query my list of objects to get a count of each of the identifying objects and cast it to an int?
我想知道是否有某种方法来查询我的对象列表以获取每个标识对象的计数并将其转换为int? Here is a example of what i am trying to do ...
这是我想要做的一个例子......
List<Object[]> controlsList = new List<Object[]>();
//Textfield = 1
//Checkbox = 2
Object[] control1 = new Object[] { 1, 1, 1, 75, 75, "txtField1", 1, 8, 10};
Object[] control2 = new Object[] { 1, 1, 1, 144, 144, "txtField2", 1, 10, 15};
Object[] control3 = new Object[] { 2, 1, 1, 50, 50, "checkYESNOy", 1, "Yes", "grpYesNo"};
Object[] control4 = new Object[] { 2, 1, 1, 50, 50, "checkYESNOn", 1, "No", "grpYesNo" };
Object[] control5 = new Object[] { 2, 1, 1, 50, 50, "checkDirectionN", 1, "North", "grpDirection" };
Object[] control6 = new Object[] { 2, 1, 1, 50, 50, "checkDirectionS", 1, "South", "grpDirection" };
Object[] control7 = new Object[] { 2, 1, 1, 50, 50, "checkDirectionW", 1, "West", "grpDirection" };
Object[] control8 = new Object[] { 2, 1, 1, 50, 50, "checkDirectionE", 1, "East", "grpDirection" };
controlsList.Add(control1);
controlsList.Add(control2);
controlsList.Add(control3);
controlsList.Add(control4);
controlsList.Add(control5);
controlsList.Add(control6);
controlsList.Add(control7);
controlsList.Add(control8);
//Some LINQ code to get back a count of all the textfields(1) and all the checkboxes(2)
// group by first item of each object[]
var groups = controlsList.GroupBy(x => x[0]);
foreach( var g in groups ){
var count = g.Count();
}
or 要么
var groups = controlsList.GroupBy(x => x[0]).ToList();
var txtCount = groups[0].Count();
var chkCount = groups[1].Count();
int textBoxCount = controlsList.Count(o => (int)o[0] == 1);
int checkBoxCount = controlsList.Count(o => (int)o[0] == 2);
You can do a GroupBy
followed by a Count
: 您可以执行
GroupBy
后跟Count
:
var query = controlsList.GroupBy(arr => (int)arr[0])
.Select(g => new { Id = g.Key, Count = g.Count() });
Now query
will contain a list of objects with each identifier and a corresponding count. 现在,
query
将包含具有每个标识符和相应计数的对象列表。
I'm a noob at linq, but maybe this will help you ... 我是linq的菜鸟,但也许这会帮助你...
var c1 = from t in controlsList
where t[0].ToString() == "1"
select t;
Console.WriteLine("text count = " + c1.Count());
var c2 = from t in controlsList
where t[0].ToString() == "2"
select t;
Console.WriteLine("check count = " + c2.Count());
How about this? 这个怎么样?
Dictionary<object, int> controls = controlsList.GroupBy(x => x[0]).ToDictionary(x => x.Key, x => x.Count());
int textBoxCount = controls[1];
int checkBoxCount = controls[2];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.