下面有2个List类,
$post_id = get_the_ID();
if (is_array(get_the_tags($post_id)) || is_object(get_the_tags($post_id))) :
foreach(get_the_tags($post_id) as $tag)
{
echo '<li><a href="' . get_tag_link($tag->term_id) . '" title="'.$tag->name.'" class="'.$tag->slug.'">' . $tag->name . '</a></li> ';
}
endif;
现在我有2个类的数据,
public class OldList
{
public string Name { get; set; }
public int Size { get; set; }
}
public class NewList
{
public string Name { get; set; }
public int Size { get; set; }
}
我想过滤var oldList = new List<OldList> {
new OldList { Name = "F1", Size = 374 },
new OldList { Name = "F2", Size = 125 }
};
var newList = new List<NewList> {
new NewList { Name = "F1", Size = 374, },
new NewList { Name = "F2", Size = 126, },
new NewList { Name = "F3", Size = 13, }
};
以检索所有新项目(例如“F3”)以及newList
Size
大于newList
的所有现有项目Size
(例如“F2”)。
对于“F1”,大小是相同的,因此我想忽略。
下面的代码给了我“F3”的结果,如何得到“F2”?
OldList
答案 0 :(得分:4)
您可以为&#34;同名,增加尺寸&#34;添加额外条件。进入.Where
语句,如下所示:
var X = newList.Where(
p => !oldList.Any(l => p.Name == l.Name)
|| oldList.Any(l => p.Name == l.Name && p.Size > l.Size)
);
答案 1 :(得分:2)
试试这个?
var result = newList.Where(i => !oldList.Any(l => i.Name == l.Name)
|| i.Size > oldList.Where(x => x.Name == i.Name).Select(x => x.Size).Max());
注意:如果您使用的是ORM,则性能不会很好,这可能无法转换为SQL。