問題描述:有一個List<string>的關鍵字列表,需要使用EF查詢某個字段下符合要求的數據,如果是普通只查詢一個關鍵字的話可以使用 (o=>o.A.Contains(keyword)),但是數組就不知所措了。
解決方法:開始百度查詢找到一篇https://blog.csdn.net/lwbjyl/article/details/51464026,方法是增加個linq組合查詢語句的方法,按照這個改了半天,最后無果,報從不知道從哪里查起的錯誤,是這句報錯的
filter2 = Expression.Call(Expression.Property(param, type.GetProperty(propertyName))
沒辦法了拿來梯子外面找找看,最后找着這個https://qawithexperts.com/questions/204/check-if-list-contains-a-value-using-lambda-and-ef-in-mvc,方法是
var TYPES = types.Split(','); //Select those list items which has typeId == SomeValueInTypeArray list = context.CompanyUsers.Where(a => TYPES.Any(type => type == a.TypeId)).ToList();
完美解決,就這么兩行,沒辦法基礎太差了。。。
if (keywords == null || keywords.Count == 0) { return new List<PublishInfoExtModel>(); } var expression = ExtLinq.True<PublishInfo>(); expression = expression.And(o => keywords.Any(k => o.PubContent.Contains(k)) && o.Status == 1);