使用Linq表达式的动态where子句具有多个表的联接

时间:2019-01-08 12:36:48

标签: linq-to-sql linq-to-entities

我有一个linq查询,但是where子句是有条件的。如果eve.EventType为null,则它将不包含在where子句中。我们如何使用Linq lambda表达式

var data= (from reg in product
                                join se in _order on reg.EventSessionId equals se.EventSessionId
                                join eve in Event on se.EventId equals eve.EventId
                                where eve.EventType == (EventType)eventType &&
                                ((!string.IsNullOrEmpty(eve.EventName) && eve.EventName.Contains(SearchText, StringComparison.OrdinalIgnoreCase))                                    
                                select (new OrderHistory
                                {
                                    RegistrationId = reg.RegistrationId,
                                    EventName = eve.EventName,
                                    EventSesionName = se.EventSesionName,                                       
                                })).ToList();

谢谢

1 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助。您应该在表达式的开头检查null。

var data= (from reg in product
                            join se in _order on reg.EventSessionId equals se.EventSessionId
                            join eve in Event on se.EventId equals eve.EventId
                            where eve.EventType != null && eve.EventType == (EventType)eventType &&
                            ((!string.IsNullOrEmpty(eve.EventName) && eve.EventName.Contains(SearchText, StringComparison.OrdinalIgnoreCase))                                    
                            select (new OrderHistory
                            {
                                RegistrationId = reg.RegistrationId,
                                EventName = eve.EventName,
                                EventSesionName = se.EventSesionName,                                       
                            })).ToList();