Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                    for (int i = 0; i < ds.Tables.Count; ++i)
                    {
                        listForHyphen.AddRange(ds.Tables[i].AsEnumerable().Distinct().Where(r => r.Field<Int64>("Action") == 1 && r.Field<string>("Terms")!= null).Select(r => "(" + r.Field<string>("Terms") + ")"));
                        listForNdash.AddRange(ds.Tables[i].AsEnumerable().Distinct().Where(r => r.Field<Int64>("Action") == 0 && r.Field<string>("Terms")!= null).Select(r => "(" + r.Field<string>("Terms") + ")"));
                    }
            }



in this i store the values in two list, using linq, any possible to short this statement , it checks column Action is equal to 1 or 0 and its null or not null, then it add the value from column Terms to list
Posted
Updated 29-Dec-14 1:19am
v2
Comments
Maciej Los 29-Dec-14 8:09am    
What you mean by "simplify"?
BillWoodruff 29-Dec-14 10:08am    
Could you please simplify what you mean by the question: "What you mean simplify ?" ?

Happy New Year ! :) Bill
Maciej Los 29-Dec-14 11:08am    
Thank you, Bill.
Happy New Year for you too!
BillWoodruff 29-Dec-14 10:11am    
Is this code working for you now ?

The assumption I see in your code is: if the first Table in ds.Tables has Rows greater than zero: then you enumerate all the Tables in ds.Tables: is that what you want ?

The LINQ statement is not that bad compared to many I have seen. Why do you think it needs to be simplified?

If you just want to simplify the entire block of code, use a foreach statement on the rows in the table like so:

C#
foreach (DataRow row in ds.Tables[0].Rows)
{
   if ( Convert.ToInt64( row["Action"].ToString()) == 1 &&                            !string.IsNullOrEmpty( row["Terms"].ToString() ))
      listForHyphen.Add( "(" + row["Terms"].ToString() + ")" );
   if( Convert.ToInt64(row[ "Action" ].ToString()) == 0 && !string.IsNullOrEmpty(row[ "Terms" ].ToString()) )
      listForNdash.Add("(" + row[ "Terms" ].ToString() + ")");
}


This will end up being more efficient than using the LINQ and is very easy to understand.
 
Share this answer
 
Why to loop through the collection of rows?

This should works too:
C#
for (int i = 0; i < ds.Tables.Count; ++i)
                    {
                        listForHyphen.AddRange(ds.Tables[i].AsEnumerable().Distinct().Where(r => r.Field<Int64>("Action") == 1 && r.Field<string>("Terms")!= null).Select(r => "(" + r.Field<string>("Terms") + ")"));
                        listForNdash.AddRange(ds.Tables[i].AsEnumerable().Distinct().Where(r => r.Field<Int64>("Action") == 0 && r.Field<string>("Terms")!= null).Select(r => "(" + r.Field<string>("Terms") + ")"));
                    }


See: List<t>.AddRange Method[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900