我有一个看起来像这样的对象
对象
每个对象都有一个PropertyListItem,其中Tag是“Revision”。我需要将“Revision”的值与列表中存储的所有其他对象进行比较,并返回“Revision”具有最高值的项目。
我认为我可以使用嵌套的for
循环构建一种方法,但我认为通过使用Linq或Lambda表达式来获取Object会更好。
我一直试图找到一种方法来自己做,但我觉得我所做的一切都是完全错误的。如果有人可以帮助我并给出一些解释,我会非常高兴。非常感谢!
编辑:
示例代码:
public MdsObject GetSoftwareObjectByName(string sPackageName)
{
GetObjectListRequest getObjectListReq = new GetObjectListRequest();
InitializeRequest(getObjectListReq);
//TODO: Are there more characters which need to be escaped in an LDAP query to DSM?
sPackageName = sPackageName.Replace("(", "\\(");
sPackageName = sPackageName.Replace(")", "\\)");
getObjectListReq.LdapQuery = "<LDAP://rootDSE>;(Name:IgnoreCase=" + sPackageName + ");;subtree";
getObjectListReq.MaxResults = -1;
GetObjectListReply getObjectListReply = AdministrationService.GetObjectList(getObjectListReq);
switch (getObjectListReply.ObjectList.Length)
{
case 0:
{ throw new ApplicationException("GetSoftwareObjectByName failed. Could not find '" + sPackageName + "'"); }
case 1:
{
MdsObject incompleteObjectFromLdap = getObjectListReply.ObjectList[0];
return GetSoftwareObjectById(incompleteObjectFromLdap.ID);
}
//more than one object was returned -> check revisions
default:
{
List<MdsObject> ListReturnedObjects = new List<MdsObject>();
for (int i = 1; i <= getObjectListReply.ObjectList.Length; i++)
{
MdsObject incompleteObjectFromLdap = getObjectListReply.ObjectList[i-1];
ListReturnedObjects.Add(GetSoftwareObjectById(incompleteObjectFromLdap.ID));
}
**Here I need to filter the objects**
throw new ApplicationException("GetSoftwareObjectByName failed. Software name '" + sPackageName + "' is not unique!");
}
}
}
答案 0 :(得分:0)
首先,将嵌套层次结构向下翻转为IEnumerable
var q = MyObject.PropertyGroupList
.SelectMany(item=>item.PropertyList); // For every Item in the GroupList, Flatten it and return the individual PropertyListItem
然后找到修订版本最高的那个
var q2 = q.OrderbyDescending(item=>item.Tag) // Order them by tag starting with the largest
.First(); // And get the first and thus biggest one.