How return the format of the size file in datatable

sblb 831 Reputation points


In the class FilePath I've a class

 public class FilePath  
        public Int64 Size { get; set; }  

This class is based to the View [dbo].[FilePath] in Sql


The datatable is returned via Ajax

I would like to return the format of the size file as below

   if (size < 1024) { size + " B" }   
                        else ((size/ 1024 )+ " KB")  

I tried to introduce this condition in class FilePath as below

 public Int64 Size  
                if (Size < 1024)  
                    return  Size + " B";  
                    return Math.Floor((Size / 1024) + " KB");  

But the format of the size is not compatible. Have you an idea how I can do it?

A set of technologies in the .NET Framework for building web applications and XML web services.
2,806 questions
No comments
{count} votes

Accepted answer
  1. AgaveJoe 19,441 Reputation points

    The only reason I can answer this post is I'm aware of your others questions related to jQuery DataTable and a SQL View.

    But the format of the size is not compatible. Have you an idea how I can do it?

    You defined Size as an Int64 but attempt to return a string. C# is a strongly type language and your approach breaks the rules. The C# Programming Guide covers these fundamental concepts.

    Use the jQuery DataTable's column configuration to create a custom string format. The BigInt type in JavaScript matches the C# Int64 type. See the JavaScript BigInt docs.

    { "data": "size", "name": "size" , 'render': function(data, type, row, meta) {  
        var size = BigInt(data);   
        if (size < 1024) { return size + ' B'}  
        return size / BigInt(1024) + ' KB';  

1 additional answer

Sort by: Oldest
  1. Zhi Lv - MSFT 21,616 Reputation points Microsoft Vendor

    Hi @sblb ,

    As AgaveJoe said, you can use the JQuery DataTable's column configuration to change the data type from Int64 to string.

    Besides, you can also create a View Model and define the Size as a string type, like this:

    public class FilePathViewModel  
        public string Size { get; set; }  

    Then, after query the SQL view and get the FilePath, you can use LINQ Query statement to convert the result from FilePath model to FilePathViewModel model, and change the Size value from Int64 to String Type, like this:

        public IActionResult Get(int id)  
            //assume this is the sql view query result.  
            var filefromdatabase = new List<FilePath>()  
                new FilePath(){ Id=101, Name="A1", Path="P1", Type="T1", Size=87122},  
                new FilePath(){ Id=101, Name="A2", Path="P2", Type="T2", Size=122},  
                new FilePath(){ Id=101, Name="A3", Path="P3", Type="T3", Size=8712222}  
            //Then, use LINQ select statement to query the data and convert the mode from FilePath to FilePathViewModel.  
            var returndata = filefromdatabase.Select(c => new FilePathViewModel()  
                Id = c.Id,  
                Name = c.Name,  
                Type = c.Type,  
                Path = c.Path,  
                Size = c.Size < 1024 ? c.Size + "B" :   
                       c.Size < 1024 * 1024? (c.Size / 1024) + "KB":  
                       (c.Size / (1024* 1024)) + "MB"  
            //Finally, return the converted data to the client and use the JQUery DataTable to display the result.  
            return Ok(returndata);  

    The result is like this:

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,