【发布时间】:2018-09-03 09:35:33
【问题描述】:
我正在尝试比较两个具有相同属性的通用项目。
如果要比较该属性,则在 true 的任何一个列表项中返回 TRUE 否则为 FALSE 我是 c# 新手,因此无法在此处发布代码,抱歉给您带来不便。
项目 1 和项目 2 具有相同的属性,但根据不同的情况计算
: 需要检查布尔属性。
public class RrmModulePermission : BaseEntity
{
public long Id { get; set; }
public int? EmployeeId { get; set; }
public int? DesignationId { get; set; }
public int ModuleId { get; set; }
public bool View { get; set; }
public bool ViewAll { get; set; }
public bool Add { get; set; }
public bool Edit { get; set; }
public bool Delete { get; set; }
public bool Import { get; set; }
public bool Export { get; set; }
public int CreatedBy { get; set; }
public DateTimeOffset CreatedOn { get; set; }
public int? UpdatedBy { get; set; }
public DateTimeOffset? UpdatedOn { get; set; }
public virtual RrmModule Modules { get; set; }
}
var list3 = new RrmModulePermission();
if (list1.View || list2.View)
{
list3.View = true;
}
if (list1.Add || list2.Add)
{
list3.Add = true;
}
if (list1.Edit || list2.Edit)
{
list3.Edit = true;
}
if(list1.Delete || list2.Delete)
{
list3.Delete = true;
}
return list3;
【问题讨论】:
-
只需将列表 1 中的每个元素与列表 2 中的每个元素进行比较。您的问题到底是什么?你试过什么?除了这个“我不能发布代码,因为我是 C# 的新手”之外?我看不出这两个事实之间有什么关系。请分享您尝试过的方法以及您遇到的困难。
-
我可以重复这个问题以确保我们清楚吗?你有两个列表——不同的类型——即
List<Foo>和List<Bar>;Foo和Bar具有一些相交的属性(按名称);您想同时遍历两个列表(假设它们具有相同的计数),并检查所有相交的bool属性,以及 either 对象是否具有true。 .. returntrue,否则false... 问题:这是每对的最终结果吗?还是整体?另外:这些类型在运行时是否已知?即你能用普通的 C# 写这个吗?还是您正在寻找一种反思方法? -
所以你有两个
List<RrmModulePermission>,你想找出关于bool属性的区别? -
@MarcGravell 是的,我正在比较具有完全相同属性的列表。说如果 List
具有 View = true,并且 List 具有 View = false 意味着在结果列表中查看= true ...对于其他属性也是如此,仅适用于布尔值!现在我希望你能理解。 -
if (SomeObject.View || OtherObject.View) { ... } else { ... }