使用LINQ在jquery AutoComplete中显示正确的Label

时间:2015-02-09 17:46:28

标签: c# jquery asp.net-mvc linq linq-to-entities

我正在使用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时,它将在表格中的任何位置查找匹配结果),例如:

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”,并且表中还有一个名为“Technology”的部门,当用户输入“te”时,它应显示“Technology”和“Terry”

2 个答案:

答案 0 :(得分:1)

你应该能够使用三元运算符在linq中模拟一个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);

答案 1 :(得分:1)

最好使用空白的字符串列表并逐个查找每个匹配的集合。

如果你想让它更具动态性,你可以使用一个使用.Concat的交互过程,一个UserProfile类的meta属性和反射。

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);