How return the format of the size file in datatable

sblb 1,166 Reputation points
2022-06-21T20:08:47.223+00:00

Hi,

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

213525-image.png

The datatable is returned via Ajax
213547-image.png

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  
        {  
            get  
            {                  
                if (Size < 1024)  
                {  
                    return  Size + " B";  
                  
                }  
                else  
                {  
                    return Math.Floor((Size / 1024) + " KB");  
                }  
            }  
        }  

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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,186 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2022-06-21T20:43:19.277+00:00

    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';  
        }   
    },  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2022-06-22T05:59:35.41+00:00

    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:

        [HttpGet("{id}")]  
        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"  
            }).ToList();  
    
            //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:
    213655-2.gif


    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,
    Dillion