4 Replies - 355 Views - Last Post: 24 June 2020 - 06:14 AM Rate Topic: -----

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,524
  • Joined: 12-January 10

since i suck at lambda thought i would ask how to convert the code:

Posted 24 June 2020 - 05:36 AM

how would i convert this to lamda because i have a ton of if else that i want to shrink down

 ddlLocation.SelectedValue = selectedInfo.Tables[0].Rows[0]["ODLocationID"].ToString();

ddlLocation.SelectedValue is the drop down list
selectedInfo is a dataset

what i want is
ddlLocation.SelectedValue = if(!string.IsNullOrEmpty(selectedInfo.Tables[0].Rows[0]["ODLocationID"].ToString())){selectedInfo.Tables[0].Rows[0]["ODLocationID"].ToString();}
else{

"-1";
}



is that possible to turn into lambda?

Is This A Good Question/Topic? 0
  • +

Replies To: since i suck at lambda thought i would ask how to convert the code:

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7404
  • View blog
  • Posts: 24,977
  • Joined: 05-May 12

Re: since i suck at lambda thought i would ask how to convert the code:

Posted 24 June 2020 - 05:44 AM

It's not a matter of using a lambda, but rather using the ternary operator:
? :

bool isSmart = true;
string intelligent = isSmart ? "Genius" : "Dunce";


Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6901
  • View blog
  • Posts: 28,559
  • Joined: 12-December 12

Re: since i suck at lambda thought i would ask how to convert the code:

Posted 24 June 2020 - 05:45 AM

ddlLocation.SelectedValue is the drop down list It is not a reference to the list, it is its ... selected value. That is, a single value. You are assigning it a string value.

Given that this is just a simple assignment, I don't see how a lambda would come into play?
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,524
  • Joined: 12-January 10

Re: since i suck at lambda thought i would ask how to convert the code:

Posted 24 June 2020 - 05:53 AM

View PostSkydiver, on 24 June 2020 - 08:44 AM, said:

It's not a matter of using a lambda, but rather using the ternary operator:
? :

bool isSmart = true;
string intelligent = isSmart ? "Genius" : "Dunce";


ill try that

it's early and ive been up since 4am because of the garbage men -- so yeah not thinking so well ha

thanks
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7404
  • View blog
  • Posts: 24,977
  • Joined: 05-May 12

Re: since i suck at lambda thought i would ask how to convert the code:

Posted 24 June 2020 - 06:14 AM

But the ternary operator will only solve the issue of figuring out what to assign. You still have the repetition of selectedInfo.Tables[0].Rows[0]["ODLocationID"].ToString().

To the first thing to do is make the code more legible is assign it to a variable, and then replace it.
var locationID = selectedInfo.Tables[0].Rows[0]["ODLocationID"].ToString();
locationID = !string.IsNullOrEmpty(value) ? value : "-1";




And then you make this into a helper method:
class Helper
{
    public static string ReplaceNullOrEmpty(string value, string replacement)
         => string.IsNullOrEmpty(value) ? replacement : value;
}


which you would call with:
var locationID = Helper.ReplaceNullOrEmpty(selectedInfo.Tables[0].Rows[0]["ODLocationID"].ToString(), "-1");



Or if believe in extension methods:
static class StringExtensions
{
    public static string ReplaceNullOrEmpty(this string value, string replacement)
         => string.IsNullOrEmpty(value) ? replacement : value;
}


which you would call like:
var locationID = selectedInfo.Tables[0].Rows[0]["ODLocationID"].ToString().ReplaceNullOrEmpty("-1");



or you could go crazy with:
static class MyExtensions
{
    public static string ReplaceNullOrEmpty(this string table, string replacement)
         => string.IsNullOrEmpty(value) ? replacement : value;

    public static string GetCellAsString(this DataTable table, int row, string column)
         => table.Rows[row][column].ToString();

    public static string GetCellAsString(this DataSet dataSet, int table, int row, string column)
         => dataSet.Tables[table].GetCellAsString(row, column);

    public static string GetCellAsDisplayString(this DataSet dataSet, int table, int row, string column, string replacement)
         => dataSet.GetCellAsString(table, row, column).ReplaceNullOrEmpty(replacement);
}


and call with:
var locationID = selectedInfo.GetCellAsString(0, 0, "ODLocationID").ReplaceNullOrEmpty("-1");


or
var locationID = selectedInfo.GetCellAsDisplayString(0, 0, "ODLocationID", "-1");


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1