我有一个sObject元素列表。我想删除具有相同记录名称的重复元素以及原始记录。
就像假设我有一个记录名称为的元素列表一样 Chair1,Chair2,Chair3,Chair4,Chair5,Chair6,Chair7,Chair1,Chair2
我想打印一个只有没有重复元素的列表。对于这种情况,我应该得到名单Chair3,Chair4,Chair5,Chair6,Chair7。
我使用以下代码来实现此功能。但我得到的记录为:Chair1,Chair2,Chair3,Chair4,Chair5,Chair6,Chair7。
在理想情况下,我们不应该获取记录Chair1,Chair2,因为这些记录已经有重复记录。
List <Chair__c> chairList = [SELECT
ID,
Name
FROM Chair__c
ORDER BY Name ASC];
System.debug('chairListOrderbyName::'+chairList);
List <String> chairNameList = new List <String>();
for(Integer i = 0; i < chairList.size();i++) {
for(Integer j = 0;j < chairList.size();j++) {
if(chairList[i].Name.equalsIgnoreCase(chairList[j].Name) && i != j) {
chairList.remove(i);
chairList.remove(j);
}
}
}
System.debug('chairList::'+chairList);
答案 0 :(得分:0)
如果您真正需要名称,则可以使用GROUP BY
和HAVING
使用纯SOQL执行此操作。像
SELECT Name
FROM Chair__c
GROUP BY Name
HAVING COUNT(Id) = 1 // only unique entries
如果你需要完整的sObjects,那么我会帮助Set<String>
并循环结果。如果名称不在集合中 - 添加它。但如果它已经存在 - &gt;删除它!
实际上,让它成为地图,类似的想法......
Map<Set, Chair__c> chairs = new Map<Set, Chair__c>();
for(Chair__c c : [SELECT ...]){
if(chairs.containsKey(c.Name)){
chairs.remove(c.Name);
} else {
chairs.put(c.Name, c);
}
}
System.debug(JSON.serializePretty(chairs));
System.debug(chairs.values());
答案 1 :(得分:0)
尝试使用File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\decorators.py" in _wrapper
62. return bound_func(*args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\decorators.py" in bound_func
58. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\edit.py" in post
172. return super().post(request, *args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\edit.py" in post
142. return self.form_valid(form)
File "C:\Users\Rahul\yantra_packs\yantra_packs\packsapp\views.py" in form_valid
227. MaterialRequest.objects.filter(id=s_o).update(is_allocated = True)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py" in filter
836. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
854. clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py" in add_q
1253. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py" in _add_q
1277. split_subq=split_subq,
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py" in build_filter
1215. condition = self.build_lookup(lookups, col, value)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py" in build_lookup
1085. lookup = lookup_class(lhs, rhs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\lookups.py" in __init__
18. self.rhs = self.get_prep_lookup()
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\lookups.py" in get_prep_lookup
68. return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\Rahul\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
947. return int(value)
Exception Type: TypeError at /employee/allotment-form/
Exception Value: int() argument must be a string, a bytes-like object or a number, not 'MaterialRequest'
代替Set<String>
。请参考下面的解决方案
List<String>