Linq 查询包含
Linq query Contains
我对此查询有疑问
var myList = (from p in db.Full
where (p.date_reception > begin & p.date_reception < end
& !p.mc_owner.Contains("NULL") & !p.mc_owner.Contains(""))
group p by p.mc_owner into g
orderby g.Count() descending
select new
{
Ownerx = g.Key,
countx = g.Count()
}).ToList();
没有添加 !p.mc_owner.Contains("") 一切正常,但是一旦我添加它就没有显示任何结果
任何有值的字符串都将包含空字符串。例如下一个测试代码将打印 true.
string a = "abc";
Console.WriteLine(a.Contains(""));
Console.ReadLine();
您可能打算写:
p.mc_owner != ""
还是写
比较好
!string.IsNullOrEmpty(p.mc_owner)
我猜想在 .contains("NULL") 中你想省略 NULL 值而不是字符串 "NULL"。我也会用 && 而不是 &。我建议重写为:
(p.date_reception > begin && p.date_reception < end
&& !string.IsNullOrEmpty(p.mc_owner))
我的做法是:
var myList = (from p in db.Full
where (p.date_reception > begin & p.date_reception < end
& !p.mc_owner.Contains("NULL") & !string.IsNullOrWhiteSpace(p.mc_owner))
group p by p.mc_owner into g
orderby g.Count() descending
select new
{
Ownerx = g.Key,
countx = g.Count()
}).ToList();
我对此查询有疑问
var myList = (from p in db.Full
where (p.date_reception > begin & p.date_reception < end
& !p.mc_owner.Contains("NULL") & !p.mc_owner.Contains(""))
group p by p.mc_owner into g
orderby g.Count() descending
select new
{
Ownerx = g.Key,
countx = g.Count()
}).ToList();
没有添加 !p.mc_owner.Contains("") 一切正常,但是一旦我添加它就没有显示任何结果
任何有值的字符串都将包含空字符串。例如下一个测试代码将打印 true.
string a = "abc";
Console.WriteLine(a.Contains(""));
Console.ReadLine();
您可能打算写:
p.mc_owner != ""
还是写
比较好!string.IsNullOrEmpty(p.mc_owner)
我猜想在 .contains("NULL") 中你想省略 NULL 值而不是字符串 "NULL"。我也会用 && 而不是 &。我建议重写为:
(p.date_reception > begin && p.date_reception < end
&& !string.IsNullOrEmpty(p.mc_owner))
我的做法是:
var myList = (from p in db.Full
where (p.date_reception > begin & p.date_reception < end
& !p.mc_owner.Contains("NULL") & !string.IsNullOrWhiteSpace(p.mc_owner))
group p by p.mc_owner into g
orderby g.Count() descending
select new
{
Ownerx = g.Key,
countx = g.Count()
}).ToList();