简体   繁体   中英

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 ?

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]
         };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM