How to store a variable when selecting a query in LINQ?

I have a LINQ query like:

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]

for Col3

and string[0]

for Col4

.

I was wondering if there is a way to call Calculate._RPMajmoSoodeGhest()

1 time and use it for Col3

and Col4

?

+3


source to share


1 answer


You can use 'let' to define a quantity that you can reference in the "select" part 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]
         };

      

+4


source







All Articles