C# 比较两个具有布尔属性的泛型项,如果其中任何一个为true,则返回true,如结果泛型列表中所示

C# 比较两个具有布尔属性的泛型项,如果其中任何一个为true,则返回true,如结果泛型列表中所示,c#,compare,C#,Compare,我试着比较两个属性相同的通用项。 如果要比较属性,在任何一个列表项中,如果为true,则意味着返回true,否则返回FALSE,我是c新手,因此无法在此处发布代码。抱歉,给您带来不便。 项目1和项目2具有相同的属性,但根据不同的情况进行计算 :需要检查布尔属性 public class RrmModulePermission : BaseEntity { public long Id { get; set; } public int? EmployeeI

我试着比较两个属性相同的通用项。 如果要比较属性,在任何一个列表项中,如果为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;

从您展示代码的方式来看,我建议您这样初始化list3

请注意,如果将任何属性的默认值设置为true,则需要替换

使用时,只需更改默认值为true的属性

例如,如果仅将默认值添加为true use


只需将列表1中的每个元素与列表2中的每个元素进行比较。你到底有什么问题?你试过什么?除此之外,我不能发布代码,因为我是C新手?我看不出这两个事实之间有什么关系。请分享你所做的尝试以及你遇到的困难。我可以重复这个问题以确保我们清楚吗?您有两个列表-不同类型-即列表和列表;Foo和Bar具有某些按名称相交的属性;您希望同时遍历这两个列表,假设它们具有相同的计数,并检查所有相交的bool属性,如果其中一个对象具有true。。。返回true,否则返回false。。。问题:这是每对的最终结果吗?还是整体?另外:这些类型在运行时是已知的吗?i、 你能用C写这个吗?或者你在寻找一种反射方法?那么你有两个列表,你想找出bool属性的差异吗?@marcGravel是的,我正在比较具有完全相同属性的列表。假设列表的View=true,列表中的View=false表示结果列表中的View=true…对于其他属性以及布尔值情况都是如此!现在我希望你能理解。如果SomeObject.View | | OtherObject.View{…}else{…}如果其中任何一个说list1.View或list2.View或两者都为null,我该如何比较?如果两个null都需要返回为false…?您是指null还是false?在C语言中,布尔不能为空,只有布尔?可以| |运算符是逻辑OR运算符。如果至少有一个值为true,则返回true。如果两者都为false,则返回false。这回答了你的问题吗?我的意思是List1和list2是泛型的,可能包含null,就像List1=null一样,List1.view也包含null。那么如何比较这种情况呢?如果list1为null,那么list1.view不为null,它就不存在。如果尝试访问null.view,它将抛出臭名昭著的NullReferenceException。不过,我会相应地编辑我的答案
// if both are null, return an object with all bools as false
if (list1 == null && list2 == null)
    return new RrmModulePermission();

// if list1 is null, set all bools to false
if (list1 == null)
    list1 = new RrmModulePermission();

// if list2 is null, set all bools to false
if (list2 == null)
    list2 = new RrmModulePermission();

var list3 = new RrmModulePermission
{
    View = list1.View || list2.View,
    ViewAll = list1.ViewAll || list2.ViewAll,
    Add = list1.Add || list2.Add,
    Edit = list1.Edit || list2.Edit,
    Delete = list1.Delete || list2.Delete,
    Import = list1.Import || list2.Import,
    Export = list1.Export || list2.Export
};

return list3;
new RrmModulePermission();
new RrmModulePermission
{
    View = false,
    ViewAll = false,
    Add = false,
    Edit = false,
    Delete = false,
    Import = false,
    Export = false
};
new RrmModulePermission { Add = false };