简体   繁体   English

如何从任何对象属性为null或为空的列表中删除All?

[英]How to RemoveAll from list where any object property is null or empty?

I have an object with many properties. 我有一个具有许多属性的对象。 I create a list of these objects and weed out the bad ones by finding cases where an important property is null or empty. 我创建了这些对象的列表,并通过查找重要属性为null或为空的情况来清除不良对象。 Like... 喜欢...

theList.RemoveAll(p => string.IsNullOrEmpty(p.ID));

How can I do a similar check but instead of checking a single property, check if any property in the object IsNullOrEmpty? 如何进行类似的检查,而不是检查单个属性,而是检查对象IsNullOrEmpty中是否有任何属性?

I've been working with Reflection to get things like 我一直在与Reflection合作,以获取诸如

object x = typeof(MyObject).GetProperties().
    Select(p => p.GetValue(exampleObject, null)).
    Where(v => string.IsNullOrEmpty(v.ToString())));

but I don't grock it well enough to put it all together. 但是我对它的把握还不够好,无法将它们放在一起。 Please set me straight. 请让我挺直。

check if any property in the object IsNullOrEmpty? 检查对象IsNullOrEmpty中是否有任何属性?

theList.RemoveAll(x => x.GetType().GetProperties()
                                  .Select(p => p.GetValue(x, null))
                                  .Any(p => p == null));

Thus it makes sense to apply String.IsNullOrEmpty only to string values, you should select only properties of string type: 因此,仅将String.IsNullOrEmpty应用于字符串值是有意义的,您应该仅选择字符串类型的属性:

List<MyObject> objects = new List<MyObject>();
// fill list

var stringProperties = typeof(MyObject)
        .GetProperties()
        .Where(p => p.PropertyType == typeof(string))
        .ToArray();

And then get filtered sequence of objects: 然后获取过滤后的对象序列:

var query = objects.Where(o =>
    stringProperties.All(p => !String.IsNullOrEmpty((string)p.GetValue(o))));

You can use same approach to remove objects from list: 您可以使用相同的方法从列表中删除对象:

objects.RemoveAll(o => 
     stringProperties.Any(p => String.IsNullOrEmpty((string)p.GetValue(o))));

WORKING SAMPLE 工作样本

C#列表<object> .RemoveAll() - 如何删除列表的子集?<div id="text_translate"><p> 我有两个具有多个匹配属性的<strong>feeds_Auto</strong>和<strong>Product</strong>类。 对于这个特殊问题,AutoID 是我需要使用的唯一字段。</p><p> 我有一个包含数百个<em>唯一</em>条目的List&lt;FeedsAuto&gt; 。 我有一个小List&lt;Product&gt;有 10、20 个<em>唯一</em>条目。 我现在想从大列表中删除小列表中的所有项目。</p><p> 如何使用 RemoveAll({lambda expression}) 来完成此操作? 我发现的所有示例都依赖于简单类型(字符串、整数等)的通用列表。</p><pre> private static int DoInserts(ref List&lt;Model.feeds_Auto&gt; source, ref List&lt;Model.Product&gt; target, Guid companyID) { List&lt;Model.Product&gt; newProductList = new List&lt;Model.Product&gt;(); List&lt;Model.Product&gt; dropSourceList = new List&lt;Model.Product&gt;(); using (var db = Helpers.GetProdDB()) { foreach (var src in source) { var tgt = target.Where(a =&gt; a.alternateProductID == src.AutoID &amp;&amp; a.LastFeedUpdate &lt; src.DateModified).FirstOrDefault(); if (tgt == null) { newProductList.Add(new Model.Product{...}); dropSourceList.Add(src); } } db.SaveChanges(); if (dropSourceList.Count &gt; 0) { source.RemoveAll(????); } } }</pre><p> 在循环中执行此操作并不困难:</p><pre> foreach (var drop in dropSourceList) { source.RemoveAll(a =&gt; a.AutoID == drop.AutoID); }</pre><p> (对我来说)循环一个集合以在每次传递中为一个项目调用 RemoveAll 只是没有意义。</p></div></object> - C# List<object>.RemoveAll() - How to remove a subset of the list?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从列表中获取对象 <object> 其中property为null且o​​rder为最低 - Get object from List<object> where property is null and order is the lowest 列表<object> .RemoveAll - 如何创建适当的谓词 - List<object>.RemoveAll - How to create an appropriate Predicate 从列表中删除全部 - RemoveAll from List C#列表<object> .RemoveAll() - 如何删除列表的子集?<div id="text_translate"><p> 我有两个具有多个匹配属性的<strong>feeds_Auto</strong>和<strong>Product</strong>类。 对于这个特殊问题,AutoID 是我需要使用的唯一字段。</p><p> 我有一个包含数百个<em>唯一</em>条目的List&lt;FeedsAuto&gt; 。 我有一个小List&lt;Product&gt;有 10、20 个<em>唯一</em>条目。 我现在想从大列表中删除小列表中的所有项目。</p><p> 如何使用 RemoveAll({lambda expression}) 来完成此操作? 我发现的所有示例都依赖于简单类型(字符串、整数等)的通用列表。</p><pre> private static int DoInserts(ref List&lt;Model.feeds_Auto&gt; source, ref List&lt;Model.Product&gt; target, Guid companyID) { List&lt;Model.Product&gt; newProductList = new List&lt;Model.Product&gt;(); List&lt;Model.Product&gt; dropSourceList = new List&lt;Model.Product&gt;(); using (var db = Helpers.GetProdDB()) { foreach (var src in source) { var tgt = target.Where(a =&gt; a.alternateProductID == src.AutoID &amp;&amp; a.LastFeedUpdate &lt; src.DateModified).FirstOrDefault(); if (tgt == null) { newProductList.Add(new Model.Product{...}); dropSourceList.Add(src); } } db.SaveChanges(); if (dropSourceList.Count &gt; 0) { source.RemoveAll(????); } } }</pre><p> 在循环中执行此操作并不困难:</p><pre> foreach (var drop in dropSourceList) { source.RemoveAll(a =&gt; a.AutoID == drop.AutoID); }</pre><p> (对我来说)循环一个集合以在每次传递中为一个项目调用 RemoveAll 只是没有意义。</p></div></object> - C# List<object>.RemoveAll() - How to remove a subset of the list? 检查对象的任何属性是否为null或为空的最佳方法是什么? - What is best way to check if any of the property of object is null or empty? 从列表中删除项目更快 <object> -RemoveAll或foreach循环? - Which is faster for removing items from a List<object> - RemoveAll or a foreach loop? 如何获取字符串属性为null或空字符串值的类列表? - How to Get List of Classes where string Property is null or Empty string value? 从对象列表中获取所有对象,其中对象属性之一在C#中为null或为空 - Get all objects from a list of objects where one of the object properties is null or empty in c# LINQ RemoveAll属性不起作用的地方 - LINQ RemoveAll where property isn't working 使用 RemoveAll 从列表中删除项目 - Removing item from list with RemoveAll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM