0
foreach (IndexingField field in document.IndexingFields)
{
    if (field.TableColumn.ToLower() == "esig"&& field.Data != null && !string.IsNullOrEmpty(field.Data.ToString()) && field.Data.ToString() != "N")
    {                               
        EsigBase64Value = field.Data.ToString();
        field.Data = "Y";                                
    }                    
}

this is for each loop i am using , i would like to convert this for each loop to lambda expression ,please help me on this

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
chandra
  • 9
  • 2
  • Why? What is the actual question? You can convert the *contents* of a loop into a lambda and call it but that lambda will look no different from a method or even a local method. If you want to clean up your code, extract the loop block's code into a separate method – Panagiotis Kanavos Jul 01 '21 at 07:47
  • What is `EsigBase64Value`? The way the code is written, it will only contain the contents of the last field after the loop. This is almost certainly a bug – Panagiotis Kanavos Jul 01 '21 at 07:48
  • [Related](https://stackoverflow.com/q/225937/1997232). TLDR; you can use `ToList().ForEach()` combo or create extension method yourself. – Sinatr Jul 01 '21 at 07:50
  • What is `field.Data`'s type? Why the calls to `ToString()`? `field.Data.ToString() != "N"` will only work if `Data` is *already* a `string`. The `if` condition could be simplified a *lot* – Panagiotis Kanavos Jul 01 '21 at 07:52
  • EsigBase64Value is string data type – chandra Jul 01 '21 at 07:54
  • field.data is of different types(int,bool,string etc..,).when table column is esig ,it will be of string data type and we need that value of esig in EsigBase64Value – chandra Jul 01 '21 at 07:57

2 Answers2

5

Loops and lambda expressions are different things with different purposes. There is no lambda expression that correlates directly with this intent, and regular LINQ methods like Select are intended to perform projections (creating new data), not mutations (changing existing data).

In short: leave the code alone - it is fine as-is. My main observation would be what the value of EsigBase64Value is intended to be when there are zero or multiple records that match the if test. You might also want to hoist the field.Data.ToString() value, so it only happens once:

var data = field.Data.ToString();
if (...)
{

}

(using data in each place where field.Data.ToString() appears)

and maybe use a case-insensitive overload of string.Equals rather than field.TableColumn.ToLower() == "esig"

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

You can use .Where

var field = document.IndexingFields.Where(f => f.TableColumn.ToLower() == "esig" && f.Data != null && !string.IsNullOrEmpty(f.Data.ToString()) && f.Data.ToString() != "N").FirstOrDefault();

And after that you can check with if condition

if (field != null)
 {
   EsigBase64Value = field.Data.ToString();
    field.Data = "Y";    
 }
H.Sarxha
  • 157
  • 1
  • 9