Check Datatable one column value with other fields Using LINQ

Rajesh_Kumar
Posted by Rajesh_Kumar under LINQ category on | Points: 40 | Views : 3663
Suppose i have Users_Name in DataTable and in ohter hand,I have Reciepient_Name is other table,and i want to check whether Reciepient_Name is there in a DataTable,if yes assign Reciepient_Name with User_name otherwise assign Reciepient_Name to empty.

Using System.LINQ; //for working with FirstOrDefault

if (!string.IsNullOrEmpty(recipient_name)) //string field coming from other table assume it will have some value
{
if (dt_user_reciepent.Rows.Count > 0) //assume that dt_user_reciepent is already filled with user_name
{
var res = (from obj in dt_user_reciepent.AsEnumerable()
where obj.Field<string>("User_name").ToLower().Contains(recpient_name)
select new
{
User_name = obj.Field<string>("User_name")
}).FirstOrDefault();

if (res != null)
{
recipient_name = res.User_ID;
}
else
{
recipient_name = string.Empty;
}
}
}

Comments or Responses

Login to post response