【问题标题】:Compare two generic items which has boolean properties, if any of them true returns true as in result generic list比较两个具有布尔属性的通用项目,如果其中任何一个为 true,则返回 true,如结果通用列表
【发布时间】: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>FooBar 具有一些相交的属性(按名称);您想同时遍历两个列表(假设它们具有相同的计数),并检查所有相交的bool 属性,以及 either 对象是否具有true。 .. return true ,否则 false... 问题:这是每对的最终结果吗?还是整体?另外:这些类型在运行时是否已知?即你能用普通的 C# 写这个吗?还是您正在寻找一种反思方法?
  • 所以你有两个List<RrmModulePermission>,你想找出关于bool属性的区别?
  • @MarcGravell 是的,我正在比较具有完全相同属性的列表。说如果 List 具有 View = true,并且 List 具有 View = false 意味着在结果列表中查看= true ...对于其他属性也是如此,仅适用于布尔值!现在我希望你能理解。
  • if (SomeObject.View || OtherObject.View) { ... } else { ... }

标签: c# compare


【解决方案1】:

从你提交代码的方式来看,我建议像这样初始化list3

// 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;

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

new RrmModulePermission();

with(你只需要把属性改成默认值true

new RrmModulePermission
{
    View = false,
    ViewAll = false,
    Add = false,
    Edit = false,
    Delete = false,
    Import = false,
    Export = false
};

例如如果只有 Add 默认为 true 使用

new RrmModulePermission { Add = false };

【讨论】:

  • 如果其中任何一个说 list1.view 或 list2.view 或两者都为 null ,我该如何比较?如果两个 null 都需要返回为 false...?
  • 您的意思是null 还是false?在 C# 中,bool 不能是 null(只有 bool? 可以)。 || 运算符是逻辑 OR 运算符。如果至少有一个值是true,则返回true。如果两者都是false,则返回false。这能回答你的问题吗?
  • 我的意思是 List1 和 list2 是通用的可能包含 null 就像 list1 = null 所以,list1.view 也是 null。那么如何比较这个案例呢?
  • 如果list1nulllist1.view 不是 null,它不存在。如果您尝试访问null.view,它将抛出臭名昭著的NullReferenceException。但是我会相应地编辑我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-06
  • 2022-12-11
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多