Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can store variable while select query in LINQ?

I have a LINQ query like this:

var q = from p in m.TblOzvs.AsEnumerable()
        where (p.OzviyatNumber == txtSearch.Text) 
        select new mylist
        {                     
            Col1 = p.OzviyatNumber.ToString(),
            Col2 = p.Name,
            Col3 = Calculate._RPMajmoSoodeGhest(p.OzviyatNumber)[1],
            Col4 = Calculate._RPMajmoSoodeGhest(p.OzviyatNumber)[0]
        };

As you can see, for Col3 and Col4 I need to call a function that returns an array of strings where string[1] is for Col3 and string[0] is for Col4.

I was wondering if there is any way to call Calculate._RPMajmoSoodeGhest() 1 time and use it for both Col3 and Col4?

like image 241
mosyflasher Avatar asked Oct 28 '25 05:10

mosyflasher


1 Answers

You can use 'let' to define a quantity that you can reference in the 'select' portion of the query:

var q = from p in m.TblOzvs.AsEnumerable()
         where (p.OzviyatNumber == txtSearch.Text) 
         let calcVal = Calculate._RPMajmoSoodeGhest(p.OzviyatNumber)
         select new mylist
         {                     
             Col1 = p.OzviyatNumber.ToString(),
             Col2 = p.Name,
             Col3 =  calcVal[1],              
             Col4 = calcVal[0]
         };
like image 97
HashPsi Avatar answered Oct 29 '25 21:10

HashPsi



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!