使用 LINQ 在 jquery 自动完成中显示正确的标签
Display the correct Label in jquery AutoComplete with LINQ
我正在测试 jQuery 的自动完成功能,但是我无法显示正确的标签。当搜索只涉及1个字段或条件时,很简单:
var model = db.UserProfiles.Where(u => u.Department.Contains(term))
.Take(10).Select(r => new
{
label = r.Department
}).Distinct();
return Json(model, JsonRequestBehavior.AllowGet);
但是,如果我想处理多个搜索条件(即当用户输入一个 searchTerm 时,它会在 table 中到处寻找匹配的结果),例如:
var model = db.UserProfiles.Where(u => (u.FirstName + " " + u.LastName).Contains(term) || u.Department.Contains(term) ||
u.JobTitle.Contains(term) || u.PhoneNumber.Contains(term) || u.Extension.Contains(term) || u.Location.Contains(term))
.Take(10).Select(r => new
{
label = //How do I display the correct label here?
}).Distinct();
return Json(model, JsonRequestBehavior.AllowGet);
预期输出:
如果有一个人的名字"Terry",并且在table中还有一个叫"Technology"的部门,当用户输入"te"时,它应该同时显示 "Technology" 和 "Terry"
您应该能够使用三元运算符在 linq to entities 中模拟 case 语句,如下所示:
var model = db.UserProfiles.Select(r => new
{
label = (u.FirstName + " " + u.LastName).Contains(term) ? (u.FirstName + " " + u.LastName) :
u.Department.Contains(term) ? u.Department :
u.JobTitle.Contains(term) ? u.JobTitle :
u.PhoneNumber.Contains(term) ? u.PhoneNumber :
u.Extension.Contains(term) ? u.Extension :
u.Location.Contains(term) ? u.Location : null
}).Where(l => l != null).Distinct().Take(10);
return Json(model, JsonRequestBehavior.AllowGet);
你最好使用一个空白的字符串列表并逐个连接每个匹配集。
如果你想让它更动态,你可以使用一个交互过程,它使用 .Concat
、UserProfile class 上的元属性和反射。
var model = (new List<string>())
.Concat(db.UserProfiles
.Where(u => (u.FirstName + " " + u.LastName).Contains(term))
.Select(u => (u.FirstName + " " + u.LastName))
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.Department.Contains(term))
.Select(u => u.Department)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.JobTitle.Contains(term))
.Select(u => u.JobTitle)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.PhoneNumber.Contains(term))
.Select(u => u.PhoneNumber)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.Extension.Contains(term))
.Select(u => u.Extension)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.Location.Contains(term))
.Select(u => u.Location)
.Distinct())
.OrderBy( s => s )//must have an orderby in order to use Take
.Take(10)
.ToList();
return Json(model, JsonRequestBehavior.AllowGet);
我正在测试 jQuery 的自动完成功能,但是我无法显示正确的标签。当搜索只涉及1个字段或条件时,很简单:
var model = db.UserProfiles.Where(u => u.Department.Contains(term))
.Take(10).Select(r => new
{
label = r.Department
}).Distinct();
return Json(model, JsonRequestBehavior.AllowGet);
但是,如果我想处理多个搜索条件(即当用户输入一个 searchTerm 时,它会在 table 中到处寻找匹配的结果),例如:
var model = db.UserProfiles.Where(u => (u.FirstName + " " + u.LastName).Contains(term) || u.Department.Contains(term) ||
u.JobTitle.Contains(term) || u.PhoneNumber.Contains(term) || u.Extension.Contains(term) || u.Location.Contains(term))
.Take(10).Select(r => new
{
label = //How do I display the correct label here?
}).Distinct();
return Json(model, JsonRequestBehavior.AllowGet);
预期输出:
如果有一个人的名字"Terry",并且在table中还有一个叫"Technology"的部门,当用户输入"te"时,它应该同时显示 "Technology" 和 "Terry"
您应该能够使用三元运算符在 linq to entities 中模拟 case 语句,如下所示:
var model = db.UserProfiles.Select(r => new
{
label = (u.FirstName + " " + u.LastName).Contains(term) ? (u.FirstName + " " + u.LastName) :
u.Department.Contains(term) ? u.Department :
u.JobTitle.Contains(term) ? u.JobTitle :
u.PhoneNumber.Contains(term) ? u.PhoneNumber :
u.Extension.Contains(term) ? u.Extension :
u.Location.Contains(term) ? u.Location : null
}).Where(l => l != null).Distinct().Take(10);
return Json(model, JsonRequestBehavior.AllowGet);
你最好使用一个空白的字符串列表并逐个连接每个匹配集。
如果你想让它更动态,你可以使用一个交互过程,它使用 .Concat
、UserProfile class 上的元属性和反射。
var model = (new List<string>())
.Concat(db.UserProfiles
.Where(u => (u.FirstName + " " + u.LastName).Contains(term))
.Select(u => (u.FirstName + " " + u.LastName))
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.Department.Contains(term))
.Select(u => u.Department)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.JobTitle.Contains(term))
.Select(u => u.JobTitle)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.PhoneNumber.Contains(term))
.Select(u => u.PhoneNumber)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.Extension.Contains(term))
.Select(u => u.Extension)
.Distinct())
.Concat(db.UserProfiles
.Where(u => u.Location.Contains(term))
.Select(u => u.Location)
.Distinct())
.OrderBy( s => s )//must have an orderby in order to use Take
.Take(10)
.ToList();
return Json(model, JsonRequestBehavior.AllowGet);