[英]distinct list LINQ Query
Trying to return a distinct list of SubString in LINQ. 试图在LINQ中返回一个不同的SubString列表。
This is returning the list but there are 100 records with the substring. 这将返回列表,但有100个记录与子字符串。 I only want to return the distinct list.
我只想返回不同的列表。
var query = from b in db.Certificates
select b.CertificateNumber.Substring(0,4);
ViewBag.BoxNumber = new SelectList(query.ToList());
I tried adding the distinct as in 我尝试添加不同的
select b.CertificateNumber.Substring(0,4).Distinct();
But it threw 但它扔了
DbDistinctExpression requires a collection argument.
DbDistinctExpression需要一个集合参数。
The way you're currently doing it, it's going to attempt to do a Distinct on the first four characters of each certificate number (stripping out duplicate characters), then return all of the results. 你目前正在这样做的方式,它将尝试对每个证书编号的前四个字符做一个区别(删除重复的字符),然后返回所有结果。 You're probably getting the error because the driver you're using is unable to create a valid SQL query that way.
您可能会收到错误,因为您使用的驱动程序无法以这种方式创建有效的SQL查询。
Instead, surround the entire first part of the query in parentheses before calling Distinct
, like this: 相反,在调用
Distinct
之前用括号括起查询的整个第一部分,如下所示:
var query = (from b in db.Certificates
select b.CertificateNumber.Substring(0,4)).Distinct();
Alternatively, using method syntax instead of a mixture of query and method syntax: 或者,使用方法语法而不是查询和方法语法的混合:
var query = db.Certificates
.Select(cer => cer.CertificateNumber.Substring(0,4))
.Distinct();
我知道了
ViewBag.BoxNumber = new SelectList(query.ToList().Distinct());
var certificatesList = (from b in db.Certificates
select b.CertificateNumber.Substring(0,4)).Distinct().ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.