1

How I can Hide Hide / Replace attribute return as json from db?

In my case if the Download_Link Attribute is null I don't want to show it in 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);

1 Answer 1

1

I Think that the Only way to do that is create two clases

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

Note that I'm supposing the Book class

Next you need to declare a delegate to Manage the conditional Logic

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

then in your select statement you need to do the following (in this case I use a List to mock the context)

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

Finally debbuging i ensured that all it's correct proff

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.