How to include conditions in Linq Enumerable

Posted by Oswaldlily under ASP.NET on 4/22/2016 | Points: 10 | Views : 202 | Status : [Member] | Replies : 2
HI All,

From the datatable, below code will make all value of the Column name "PERSON" as string.Empty.
In my case, if it contains value, should not change to string.Empty.. (value should remain same)
I m trying to write conditions between the folowing foreach.

Table.AsEnumerable().ToList().ForEach(p => p.SetField<string>("PERSON"," "));


Expecting like : Table.AsEnumerable().ToList().ForEach(p => if(p.PERSON ==null or 0) p.SetField<string>("PERSON"," "));




Responses

Posted by: Jayakumars on: 4/25/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi

I change the code check and let me know.



protected void bt1_Click(object sender, EventArgs e)
{
RunTest();
}

private void RunTest()
{
List empcoll = new List();
empcoll.Add(new Employee() { EmployeeNumber = "1111", HireDate = DateTime.Now });
empcoll.Add(new Employee() { EmployeeNumber = "3333", HireDate = DateTime.Now });
empcoll.Add(new Employee() { EmployeeNumber = "2222", HireDate = null });
empcoll.Add(new Employee() { EmployeeNumber = "4444", HireDate = null });
//Here's the "money" line!
empcoll.Where(x => x.HireDate.HasValue == false).ToList().ForEach(item => ReportEmployeeWithMissingHireDate(item.EmployeeNumber));
//if(item ==null) throw exception
}

public string ReportEmployeeWithMissingHireDate(string EmployeeNumber)
{
if (EmployeeNumber==null)
{
// pass your error string here...
}
return null;
}


Mark as Answer if its helpful to you

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Professionaluser on: 4/25/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
The below link might help you to handle NULL List
https://lostechies.com/jimmybogard/2009/12/08/linq-query-operators-and-null-lists/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response