Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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#_Asp.net Mvc - Fatal编程技术网

C# 从数据库返回Json结果时隐藏数据库属性

C# 从数据库返回Json结果时隐藏数据库属性,c#,asp.net-mvc,C#,Asp.net Mvc,如何从db中隐藏/替换返回为json的属性 在我的例子中,如果下载链接属性为空,我不想在json中显示它 var x = db.Books.OrderByDescending(p=>p.Book_id).Take(200).ToList(); return Json(x.Select(p => new { Title = p.Book_name,

如何从db中隐藏/替换返回为json的属性

在我的例子中,如果下载链接属性为空,我不想在json中显示它

var x = db.Books.OrderByDescending(p=>p.Book_id).Take(200).ToList();   
            
            return Json(x.Select(p => new
            {
                Title = p.Book_name,
                ISBN = p.ISBN,
                Edition = p.EditionValue,
                Pages = p.PageCount,
                Authors = p.Author_name,
                Download_Link = p.Pdf_Path_Local.Replace("~","https://s1.bookz.cc")
                
            }
            ), JsonRequestBehavior.AllowGet);

我认为唯一的方法就是创建两个类别

public class JsonWithoutLink
    {
        public string Title { get; set; }
        public string ISBN { get; set; }
        public string Edition { get; set; }
        public int Pages { get; set; }
        public string Authors { get; set; }
    }

    public class JsonWithLink : JsonWithoutLink
    {
        public string Download_Link { get; set; }
    }

    public class Book
    {
        public int Book_id { get; set; }
        public string Book_name { get; set; }
        public string ISBN { get; set; }
        public string EditionValue { get; set; }
        public int PageCount { get; set; }
        public string Author_name { get; set; }
        public string Pdf_Path_Local { get; set; }
    }

请注意,我假设的是Book类

接下来,您需要声明一个委托来管理条件逻辑

Func<Book, JsonWithoutLink> ConditionalSelection = delegate (Book b)
        {
            if (string.IsNullOrEmpty(b.Pdf_Path_Local))
            {
                return new JsonWithoutLink
                {
                    Authors = b.Author_name,
                    Edition = b.EditionValue,
                    ISBN = b.ISBN,
                    Pages = b.PageCount,
                    Title = b.Book_name
                };
            }
            else
            {
                return new JsonWithLink
                {
                    Authors = b.Author_name,
                    Edition = b.EditionValue,
                    ISBN = b.ISBN,
                    Pages = b.PageCount,
                    Title = b.Book_name,
                    Download_Link = b.Pdf_Path_Local.Replace("~", "https://s1.bookz.cc")
                };
            }
        };
Func ConditionalSelection=委托(b册)
{
if(string.IsNullOrEmpty(b.Pdf\u Path\u Local))
{
返回新的JsonWithoutLink
{
作者=b.作者姓名,
Edition=b.EditionValue,
ISBN=b.ISBN,
Pages=b.PageCount,
Title=b.书名
};
}
其他的
{
返回新的JsonWithLink
{
作者=b.作者姓名,
Edition=b.EditionValue,
ISBN=b.ISBN,
Pages=b.PageCount,
Title=b.书名,
下载\u Link=b.Pdf\u路径\u本地。替换(“~”,”https://s1.bookz.cc")
};
}
};
然后在select语句中,需要执行以下操作(在本例中,我使用一个列表模拟上下文)

public IEnumerable get()
{
变量列表=新列表
{
新书
{
作者_name=“Max”,
Book_id=1,
Book_name=“Book 1”,
EditionValue=“asdf”,
ISBN=“1234”,
PageCount=3,
Pdf\u Path\u Local=“~/somePath”
},
新书
{
作者_name=“Max”,
Book_id=1,
Book_name=“Book 1”,
EditionValue=“asdf”,
ISBN=“1234”,
页面计数=3
}
};
返回getjson(list.AsQueryable().OrderByDescending(b=>b.Book_id));
}
公共IEnumerable getjson(IOrderedQueryable图书)
{
var列表=书籍
.Select(ConditionalSelection.ToList();
退货清单;
}
最后,我确保了所有这些都是正确的

        public IEnumerable<JsonWithoutLink> get()
        {
            var list = new List<Book>
            {
                new Book
                {
                    Author_name = "Max",
                    Book_id = 1,
                    Book_name = "Book 1",
                    EditionValue = "asdf",
                    ISBN = "1234",
                    PageCount = 3,
                    Pdf_Path_Local = "~/somePath"
                },
                new Book
                {
                    Author_name = "Max",
                    Book_id = 1,
                    Book_name = "Book 1",
                    EditionValue = "asdf",
                    ISBN = "1234",
                    PageCount = 3
                }
            };

            return getJsons(list.AsQueryable().OrderByDescending(b => b.Book_id));
        }
        public  IEnumerable<JsonWithoutLink> getJsons(IOrderedQueryable<Book> books)
        {
            var list =  books
                .Select(ConditionalSelection).ToList();
            return list;
        }