从列表中获取唯一的详细信息 [英] Get unique details from list
问题描述
我在List< Items>中有以下项目对象.
obj1-ItemCode = 001;
名称="aa";
价格= 100;
PoNo = 1;
obj2-ItemCode = 002;
名称="bb";
价钱= 200;
PoNo = 2;
obj3-ItemCode = 001;
名称="aa";
价格= 100;
PoNo = 3;
obj4-ItemCode = 001;
名称="aa";
价钱= 250;
PoNo = 4;
现在,我想在PoNo中采用唯一的ItemCode和Price.如果相同的ItemCode和Price包含在不同的PoNo中,则应采用最大PoNo.
最终输出列表应包含.
obj2,obj3,obj4
I have following item objects in a List<Items>.
obj1 - ItemCode=001;
Name="aa";
Price=100;
PoNo=1;
obj2 - ItemCode=002;
Name="bb";
Price=200;
PoNo=2;
obj3 - ItemCode=001;
Name="aa";
Price=100;
PoNo=3;
obj4 - ItemCode=001;
Name="aa";
Price=250;
PoNo=4;
Now I want to take unique ItemCode and Price in PoNo. If same ItemCode and Price contains in different PoNo, Max PoNo should take.
Finaly output list should contains.
obj2,obj3,obj4
推荐答案
您做错了.如我所见,您需要通过ItemCode
快速搜索.如果是这样,请不要使用List
.
假设声明类似于
You are doing it wrong. As I can see, you need fast search byItemCode
. If so, don''t useList
.
Assuming the declaration is something like
class Item {
internal int ItemCode { get; set; }
internal string Name { get; set; }
//...
}
将您的项目存储在System.Collections.Generic.Dictionary<ItemCode, Item>
中,即Item
的由ItemCode
索引的字典,请参见 http://msdn.microsoft.com/en-us/library/xfhwa508.aspx [ ^ ].
或者,从相同的名称空间使用通用SortedDictionary
或SortedList
.您希望获得任何功能上的差异,仅在内存开销和速度之间进行不同的折衷,在所有情况下都相当不错,并且复杂度为O(1)(有关"Big O Notation"的理解,请参见http://en.wikipedia.org/wiki/Big_O_notation [ ^ ]).
store your items in System.Collections.Generic.Dictionary<ItemCode, Item>
, that is, the dictionary of Item
s indexed by ItemCode
, see http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^].
Alternatively, use generic SortedDictionary
or SortedList
from the same name space. You want get any functional difference, only different trade-off between memory overhead and speed, pretty good in all cases, with complexity of O(1) (for understanding "Big O Notation", see http://en.wikipedia.org/wiki/Big_O_notation[^]).
请对此进行详细说明.
在我看来,您想要一个最终结果列表,该列表包含每个对象中的一个,该对象具有唯一的ItemCode ...或...如果存在多个具有相同ItemCode的Item,并且具有除PoNo匹配Value之外的所有内部字段. ..然后您要选择多个项目之一.
并且,当两个(或多个)项目以PoNo以外的其他方式进行匹配时:您需要明确...如果仅选择其中一个,则该选择的依据是:首先?最后吗?
基于观察到Item1和Item3共享除PoNo之外的所有值,并且Item1不在最终列表结果中的情况,我假设您只想获取Itemcode 001的最后一个唯一实例...是正确的吗?
除了通用List以外,其他某种形式的数据结构可能也很适合,但是为了讨论这一点,我认为我们需要了解一些有关如何访问和使用对象的知识:最常用的使用类型是什么?
如此处所示,如果PoNo是每个对象的唯一标识符,则表示开始".
Please describe this in more detail.
It appears to me you want a final result list that includes one of every object which either has a unique ItemCode ... or ... if there are multiple Items with the same ItemCode and which have all internal fields except PoNo match in Value ... then you wish to select one of those multiple items.
And, when two (or more) Items match in every way but the PoNo: you need make explicit ... if you are only choosing one of them ... what the basis for that choice is: first ? last ?
Based on the observation that Item1 and Item3 share all values except PoNo, and Item1''s not in your final list result, I assume you want to take only the last unique instance with Itemcode 001 ... is that correct ?
Some other form of data structure than generic List may be a good fit here, but in order to discuss that, I think we need to know something about how the objects are accessed and used: what''s the most frequent type of use here ?
If, as shown here, PoNo is a unique identifier for each object, that''s a start.
var result = (
from f in foos
from s in f.MyStrings
select s).Distinct();
// Which is absoulutely equivalent to:
var theSameThing = foos.SelectMany(i => i.MyStrings).Distinct();
// pick the one you think is more readable.
这篇关于从列表中获取唯一的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!