Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in getting Minimum length using LINQ?

Tags:

c#

linq

Here i am retrieving minimum size of a text file inside a directory.But it gives 0 as a minimum size.But there is no 0 kb file inside that directory.

var queryList3Only= (from i in di.GetFiles("*.txt", SearchOption.AllDirectories)
                     select i.Length / 1024).Min();
dest.WriteLine(queryList3Only.ToString()+" Kb");

Any Suggestion?

like image 930
bala3569 Avatar asked Aug 26 '26 23:08

bala3569


2 Answers

you need to select doubles not int's. if filesize is < 1024 then you will end with size 0

var queryList3Only= (from i in di.GetFiles("*.txt", SearchOption.AllDirectories)
                     select (double)i.Length / 1024).Min();
like image 65
Stecya Avatar answered Aug 29 '26 12:08

Stecya


If you have any files less that 1024 bytes then they will appear as zero as your integer division will be truncated.

1023 / 1024 = 0

You may find casting the values to doubles will get you an answer between 0 and 1.

like image 26
Colin Mackay Avatar answered Aug 29 '26 13:08

Colin Mackay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!