问题描述
我有一个Linq查询.在这种情况下,我使用包含列表的列表来过滤一些记录.查询需要在任何一个列表值(100或110或120)中寻找不为空的值.
I have a Linq Query. In that I am using a list with contains to filter some records. The query needs to look for the values that is not null in any one of the list values (100 or 110 or 120).
列出值
List splitted = {"100", "110", "120"}
查询
model = (from line in db.Bibs
where line.TNo == "245"
&& (line.NrmVal.StartsWith(Val) || line.NrmVal.Contains(" " + Val))
select new MyModel
{
Author = (from a in db.Bibs
where a.BId == line.Bid
&& splitted.Contains(a.TNo)
&& a.NrmVal != null
select a.NrmVal).FirstOrDefault()
}).ToList();
感谢提供解决方案的任何帮助.
Any help in providing the solution is appreciated.
谢谢
推荐答案
尝试使用等效的Lambda
Try this Lambda equivalent
var query = db.Bibs.Where(x => x.TNo == "245");
query = query.Where(x => x.NrmVal.StartsWith(Val) || x.NrmVal.Contains(" " + Val));
query = query.Select(x => new {
Author = db.Bibs.Where(a => a.BId == x.BId && a.NrmVal != null && splitted.Contains(a.TNo)).FirstOrDefault()
});
如果您仍然遇到相同的问题,那么我会注释掉每个部分,直到您发现哪个部分不起作用为止.我优化了Author
查询,以在查找列表之前先检查null
.
If you still get the same problem then I would comment out each section until you find which section doesn't work. I optimized the Author
query to check for null
first before looking up the list.
而且,它使代码更易于管理,并且更具可读性.
Also, it makes the code a bit easier to manage and arguable more readable.
这篇关于包含在LINQ查询中并检查不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!