Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在数据库端筛选选定的JSon字段_C#_Json_Linq - Fatal编程技术网

C# 在数据库端筛选选定的JSon字段

C# 在数据库端筛选选定的JSon字段,c#,json,linq,C#,Json,Linq,这是我的DAL方法: public List<Schedule> GetSchedulesWithProfiles(int displayStart, int displayLength, out int allDataCount, out int filteredDatacount, string searchParam = "", string searchDir = "") { using (var context = new Appli

这是我的DAL方法:

public List<Schedule> GetSchedulesWithProfiles(int displayStart, int displayLength, out int allDataCount, out int filteredDatacount, string searchParam = "", string searchDir = "")
        {
            using (var context = new ApplicationDbContext())
            {
                var schedules = context.Schedules.Include(x => x.Profile).Include(x => x.VacationType);
                allDataCount = schedules.Count();
                if (!string.IsNullOrEmpty(searchParam))
                {
                    schedules = schedules.Where(c => c.Data.Contains(searchParam));     
                }
                filteredDatacount = schedules.Count();
                if (searchDir == "asc" || String.IsNullOrEmpty(searchDir))
                    schedules = schedules.OrderBy(x => x.Data).Skip(displayStart).Take(displayLength);
                else
                    schedules = schedules.OrderByDescending(x => x.Data).Skip(displayStart).Take(displayLength);
                return schedules.ToList();
              }
        }

这只是数据库中的
nvarchar
。我需要按
FirstName
和/或
LastName
进行过滤。我如何实施它?在数据库端的IQueryable LINQ中是否存在反序列化对象的方法?

目前SQL server中没有对JSON数据的本机支持:

此外,您不能在数据库端反序列化字符串。您需要从数据库中获取字符串,然后在本地反序列化它

我建议您创建三列FirstName、LastName和Position,而不是存储序列化数据。或者,如果您希望数据具有灵活性,请考虑使用适当的数据库(例如)

值得一读:。正如Phil所说,解析JSON在SQL中是不合适的。但您可以使用他的SQL函数将JSON数据转换为表格式


旁注:

要获取所有计划的计数,请使用
allDataCount=context.schedules.count()
。如果只是查找计数,则不需要在生成的查询中包含相关实体

此行
filteredDatacount=schedules.Count()
执行一个您不需要的查询。获取所有过滤实体后,只需获取计数:

var filteredSchedules = schedules.ToList();
filteredDatacount = filteredSchedules.Count;
return filteredSchedules;

Sergey,在数据库中使用JSON不是我的主意。但现在我需要和它一起工作。也许,在本地反序列化它不是一个好方法,因为我会遇到性能问题。这是不是一个不在数据库端获取计数(如果我有大数据~100000)的好方法?@Alexander如果你有100000个实体,那么
ToList()
不是最好的方法。我怀疑你是否需要客户上的所有这些实体。调用
ToList()
query后,将创建新的列表,并将查询结果中的每一行映射到内存中的实体上。因此,
filteredSchedules.Count
将不花费任何费用。它将只返回内存中列表的长度。
var filteredSchedules = schedules.ToList();
filteredDatacount = filteredSchedules.Count;
return filteredSchedules;