简体   繁体   English

如何编写 linq 查询以排除某些记录?

[英]How to write a linq query to exclude some of the records?

This is my LINQ这是我的 LINQ

IList<string> ExceptList = new List<string>() { "045C388E96", "C9B735E166", "02860EB192", "2401016471" };

                var listusers = context.USER_INFO.Where(x => x.ACTIVATED
                                    && x.COMP.EQUIPMENT.Count(y => y.STATUS == (int)STATUSEQ.ACTIVE) > 0
                                    && (x.LAST_LOGIN < time) 
                                    && !ExceptList.Contains(x.COMP.CODE)
                                    && !x.IS_LOCK
                                    || !x.COMP.IS_LOCK)
                    .Select(x => new EmailOutOfDateLoginModel
                    {
                        COMPCode = x.COMP.CODE,
                        First_Name = x.FIRST_NAME,
                        Last_Name = x.LAST_NAME,
                        Total_EQ = x.COMP.EQUIPMENT.Count(y => y.STATUS == (int)STATUSEQ.ACTIVE),
                        User_Email = x.USER_EMAIL
                    }).ToList();

I am not sure why my ExceptList is not working.我不确定为什么我的 exceptList 不起作用。 I want to exclude any record that contaisn any of the CODE in the ExceptList我想排除包含异常列表中任何代码的任何记录

Put parentheses around the expressions containing the && logic.在包含 && 逻辑的表达式周围加上括号。 The || || at the end is only matched with the.x.IS_LOCK ||.x.COMP.IS_LOCK otherwise.最后只匹配.x.IS_LOCK ||.x.COMP.IS_LOCK 否则。

According your linq all records where (.x.COMP.IS_LOCK==true) will be included in the query: Try this "where" part:根据您的 linq 查询中将包含 (.x.COMP.IS_LOCK==true) 的所有记录:试试这个“where”部分:

.Where(x => x.ACTIVATED
&& x.COMP.EQUIPMENT.Count(y => y.STATUS == (int)STATUSEQ.ACTIVE) > 0
&& (x.LAST_LOGIN < time) 
&& !ExceptList.Contains(x.COMP.CODE) 
&& !(x.IS_LOCK && x.COMP.IS_LOCK))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM