C# 将 string[] 数组与通用对象列表进行比较
如何使用 LINQ 将 string[] 数组中的项目与包含对象的通用列表进行比较?
这个通用列表包含名为 picInfo 的对象。 picinfo 类如下所示:
[ProtoContract]
public class PicInfo
{
[ProtoMember(1)]
public string fileName { get; set; }
[ProtoMember(2)]
public string completeFileName { get; set; }
[ProtoMember(3)]
public string filePath { get; set; }
[ProtoMember(4)]
public byte[] hashValue { get; set; }
public PicInfo() { }
}
string[] 数组包含图片的文件路径。我试图检查通用列表是否已包含该特定图片的路径。
图片的通用列表如下所示:
List<PicInfo> pi = new List<PicInfo>();
如果通用列表已经有该图片,我想从 string[] 数组中删除该项目。
我可以使用 foreach 循环来做到这一点,并逐一比较项目。但是我如何使用 linq 来做到这一点?
提前致谢!
how can i compare the items in a string[] array against a generic list that contains objects using LINQ?
this generic list contains objects called picInfo. picinfo class looks like this:
[ProtoContract]
public class PicInfo
{
[ProtoMember(1)]
public string fileName { get; set; }
[ProtoMember(2)]
public string completeFileName { get; set; }
[ProtoMember(3)]
public string filePath { get; set; }
[ProtoMember(4)]
public byte[] hashValue { get; set; }
public PicInfo() { }
}
the string[] array contains filepaths of pictures. im trying to check if the generic list already contains the path of this particular picture.
the generic list of the pictures looks like this:
List<PicInfo> pi = new List<PicInfo>();
if the generic list already has that picture, i'd like to remove the item from the string[] array.
i can do this using the foreach loop and compare the items 1 by 1. but how can i do this using linq?
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只确定重复项,然后删除它们:
但是更干净且更具可读性的方法是使用 Except() 并创建一个新数组,尽管这对于创建数组来说会增加一点开销:
You could just determine the duplicates and then remove them:
But a cleaner approach and more readable would be using
Except()
and creating a new array, although this is a little more overhead for the array creation: