Optimize code block for LINQ and non LINQ I have 2 code block to do same a work and have same target create some thing, but I don't know which block is the best (I mean LINQ or non-LINQ)?

First of all, can anyone tell me how to optimize on the code blocks?  And the lastly, help me find the best style between LINQ and non-LINQ:

First block is:

if (tblone.Rows.Count &gt; 0)
        {
            tblone.Rows.Cast&lt;DataRow&gt;().Where(x =&gt; (string)x["id"] == "true").ToList()
                .ForEach(x =&gt; tbltwo.Rows.Cast&lt;DataRow&gt;().Where(y =&gt; y["category"] == x["cate"]).ToList().ForEach(
                    z =&gt;
                    {
                        tblthree.Rows.Cast&lt;DataRow&gt;()
                                          .ToList&lt;DataRow&gt;()
                                          .ForEach(k =&gt; datatemp.Rows.Add(k["m"].ToString()));
                        tblfour.Rows.Cast&lt;DataRow&gt;()
                            .ToList&lt;DataRow&gt;()
                            .ForEach(k =&gt; datatemp.Rows.Add(k["m"].ToString()));
                    }));
        }


Second block is:

if (tblone.Rows.Count &gt; 0)
        {
            for (var idx = 0; idx &lt; tblone.Rows.Count; idx++)
            {
                if (bool.Parse(tblone.Rows[idx]["subset"].ToString()))
                {
                    for (var kdx = 0; kdx &lt; tbltwo.Rows.Count; kdx++)
                    {
                        if (tbltwo.Rows[kdx]["category"].ToString() == tblone.Rows[idx]["cate"].ToString())
                        {
                            for (var zdx = 0; zdx &lt; tblthree.Rows.Count; zdx++)
                                datatemp.Rows.Add(tblthree.Rows[zdx]["m"].ToString());
                            for (var zdx = 0; zdx &lt; tblfour.Rows.Count; zdx++)
                                datatemp.Rows.Add(tblfour.Rows[zdx]["m"].ToString());
                        }
                    }
                }
            }
        }
First - if you are working with DataTables, then you can use Linq to DataSet, which has nice extension methods to get field values.

Now let's review logic of your code. You are not using any data from tblone and tbltwo in your result. Actually you only finding joined categories between these two tables, and for each matched rows pair you are adding all m values from tblthree and tblfour to result. I find this logic pretty strange. Maybe you are expecting only single match here. Then you will need boolean result (match found or not). But basically you need count of joined rows:

var matchedCategoriesCount = 
            (from one in tblone.AsEnumerable().Where(r =&gt; r.Field&lt;bool&gt;("id"))
             join two in tbltwo.AsEnumerable()
                on one.Field&lt;string&gt;("cate") equals two.Field&lt;string&gt;("category")
             select one).Count();


Same with lambda syntax if you find it more readable:

var matchedCategoriesCount = 
            tblone.AsEnumerable().Where(r =&gt; r.Field&lt;bool&gt;("id"))
                  .Join(tbltwo.AsEnumerable(),
                        one =&gt; one.Field&lt;string&gt;("cate"),
                        two =&gt; two.Field&lt;string&gt;("category"),
                        (one, two) =&gt; one)
                  .Count();


Then you can select m values from tblthree and tblfour N times and create new DataTable:

// if (matchedCategoriesCount &gt; 0)
var values = tblthree.AsEnumerable().Concat(tblfour.AsEnumerable())
                     .Select(r =&gt; r.Field&lt;string&gt;("m")))
                     .ToArray();


You can even use simple loop here:

for(int i = 0; i &lt; matchedCategoriesCount; i++)
   foreach(var value in values)
       datatemp.Rows.Add(value);


And last - don't use variable names like tblone, tbltwo, tblthree etc. These names does not explain what data these variables hold. Use names from domain of your application.