@T.Zacks , you could try the following code to join two datatable without LINQ.
internal class Program
{
static void Main(string[] args)
{
List<Bogey> Bogeylist = new List<Bogey>();
Bogeylist.Add(new Bogey() { Section = "test1", LineItem = "1001" });
Bogeylist.Add(new Bogey() { Section = "test2", LineItem = "1002" });
Bogeylist.Add(new Bogey() { Section = "test4", LineItem = "1004" });
Bogeylist.Add(new Bogey() { Section = "test5", LineItem = "1005" });
List<LII> list = new List<LII>();
list.Add(new LII() { StandrdFormula = "Hello", Section = "test1", LI = "1001", CrossCalc1Q = "111" ,AllowComma=true});
list.Add(new LII() { StandrdFormula = "Hi", Section = "test2", LI = "1002", CrossCalc1Q = "222" ,AllowComma=false});
list.Add(new LII() { StandrdFormula = "Greeting", Section = "test3", LI = "1003", CrossCalc1Q = "333" ,AllowComma=true});
list.Add(new LII() { StandrdFormula = "Greeting", Section = "test4", LI = "1004", CrossCalc1Q = "333", AllowComma = true });
var BogeyConfigList = (from bogyconfiglist in Bogeylist.AsEnumerable() //10QKbogey data read here
join LiList in list.AsEnumerable() //~10QKConfig.liconfg file read here
on new
{
val = bogyconfiglist.LineItem.Trim().ToUpper(),
val1 = bogyconfiglist.Section.Trim().ToUpper()
}
equals new
{
val = LiList.LI.Trim().ToUpper(),
val1 = LiList.Section.Trim().ToUpper()
}
into conbogylist
from confg in conbogylist.DefaultIfEmpty()
select new QCHelper()
{
Section = bogyconfiglist.Section,
Li = bogyconfiglist.LineItem,
AllowComma = confg == null ? false : confg.AllowComma,
CrossCalc1Q = confg == null ? string.Empty : (confg.CrossCalc1Q == null ? "" : confg.CrossCalc1Q.Replace("~9999", string.Empty).Trim()),
FormulaLiConfig = confg == null ? string.Empty : (confg.StandrdFormula == null ? "" : confg.StandrdFormula.Replace("~9999", string.Empty).Trim()),
}).Distinct().ToList<QCHelper>();
// New method without using linq
List<QCHelper> helperlist = new List<QCHelper>();
var newlist = new List<Bogey>();
foreach (var bogyconfiglist in Bogeylist)
{
foreach (var LiList in list)
{
if (bogyconfiglist.LineItem == LiList.LI && bogyconfiglist.Section == LiList.Section)
{
QCHelper helper = new QCHelper();
helper.Section = LiList.Section;
helper.Li = LiList.LI;
helper.CrossCalc1Q = LiList == null ? string.Empty : (LiList.CrossCalc1Q == null ? "" : LiList.CrossCalc1Q.Replace("~9999", string.Empty).Trim());
helper.AllowComma = LiList == null ? false : LiList.AllowComma;
helper.FormulaLiConfig = LiList == null ? string.Empty : (LiList.StandrdFormula == null ? "" : LiList.StandrdFormula.Replace("~9999", string.Empty).Trim());
helperlist.Add(helper);
newlist.Add(bogyconfiglist);
}
}
}
var leftlist = Bogeylist.Except(newlist).ToList();
foreach (var bogyconfiglist in leftlist)
{
QCHelper helper = new QCHelper();
helper.Section = bogyconfiglist.Section;
helper.Li = bogyconfiglist.LineItem;
helper.CrossCalc1Q = "";
helper.AllowComma = false;
helper.FormulaLiConfig = "";
helperlist.Add(helper);
}
foreach (var item in BogeyConfigList)
{
string result = string.Format("{0}-{1}-{2}-{3}-{4}", item.Section, item.Li, item.CrossCalc1Q, item.AllowComma, item.FormulaLiConfig);
Console.WriteLine(result);
}
Console.WriteLine("****************");
foreach (var item in helperlist)
{
string result = string.Format("{0}-{1}-{2}-{3}-{4}", item.Section, item.Li, item.CrossCalc1Q, item.AllowComma, item.FormulaLiConfig);
Console.WriteLine(result);
}
Console.ReadKey();
}
}
public class Bogey
{
public string LineItem { get; set; }
public string Section { get; set; }
}
public class LII
{
public string LI { get; set; }
public string Section { get; set; }
public string StandrdFormula { get; set; }
public string CrossCalc1Q { get; set; }
public bool AllowComma { get; set; }
}
public class QCHelper
{
public string Section { get; set; }
public string Li{ get; set; }
public string CrossCalc1Q { get; set; }
public bool AllowComma { get; set; }
public string FormulaLiConfig { get; set; }
}
For testing your code, I created two list and three classes.
Result:
Update for comparing two methods by using stopwatch:
internal class Program
{
static void Main(string[] args)
{
Stopwatch stopwatch1 = new Stopwatch();
Stopwatch stopwatch2= new Stopwatch();
List<Bogey> Bogeylist = new List<Bogey>();
Bogeylist.Add(new Bogey() { Section = "test1", LineItem = "1001" });
Bogeylist.Add(new Bogey() { Section = "test2", LineItem = "1002" });
Bogeylist.Add(new Bogey() { Section = "test4", LineItem = "1004" });
Bogeylist.Add(new Bogey() { Section = "test5", LineItem = "1005" });
List<LII> list = new List<LII>();
list.Add(new LII() { StandrdFormula = "Hello", Section = "test1", LI = "1001", CrossCalc1Q = "111" ,AllowComma=true});
list.Add(new LII() { StandrdFormula = "Hi", Section = "test2", LI = "1002", CrossCalc1Q = "222" ,AllowComma=false});
list.Add(new LII() { StandrdFormula = "Greeting", Section = "test3", LI = "1003", CrossCalc1Q = "333" ,AllowComma=true});
list.Add(new LII() { StandrdFormula = "Greeting", Section = "test4", LI = "1004", CrossCalc1Q = "333", AllowComma = true });
stopwatch1.Start();
var BogeyConfigList = (from bogyconfiglist in Bogeylist.AsEnumerable() //10QKbogey data read here
join LiList in list.AsEnumerable() //~10QKConfig.liconfg file read here
on new
{
val = bogyconfiglist.LineItem.Trim().ToUpper(),
val1 = bogyconfiglist.Section.Trim().ToUpper()
}
equals new
{
val = LiList.LI.Trim().ToUpper(),
val1 = LiList.Section.Trim().ToUpper()
}
into conbogylist
from confg in conbogylist.DefaultIfEmpty()
select new QCHelper()
{
Section = bogyconfiglist.Section,
Li = bogyconfiglist.LineItem,
AllowComma = confg == null ? false : confg.AllowComma,
CrossCalc1Q = confg == null ? string.Empty : (confg.CrossCalc1Q == null ? "" : confg.CrossCalc1Q.Replace("~9999", string.Empty).Trim()),
FormulaLiConfig = confg == null ? string.Empty : (confg.StandrdFormula == null ? "" : confg.StandrdFormula.Replace("~9999", string.Empty).Trim()),
}).Distinct().ToList<QCHelper>();
stopwatch1.Stop();
stopwatch2.Start();
List<QCHelper> helperlist = new List<QCHelper>();
var newlist = new List<Bogey>();
foreach (var bogyconfiglist in Bogeylist)
{
foreach (var LiList in list)
{
if (bogyconfiglist.LineItem == LiList.LI && bogyconfiglist.Section == LiList.Section)
{
QCHelper helper = new QCHelper();
helper.Section = LiList.Section;
helper.Li = LiList.LI;
helper.CrossCalc1Q = LiList == null ? string.Empty : (LiList.CrossCalc1Q == null ? "" : LiList.CrossCalc1Q.Replace("~9999", string.Empty).Trim());
helper.AllowComma = LiList == null ? false : LiList.AllowComma;
helper.FormulaLiConfig = LiList == null ? string.Empty : (LiList.StandrdFormula == null ? "" : LiList.StandrdFormula.Replace("~9999", string.Empty).Trim());
helperlist.Add(helper);
newlist.Add(bogyconfiglist);
}
}
}
var leftlist = Bogeylist.Except(newlist).ToList();
foreach (var bogyconfiglist in leftlist)
{
QCHelper helper = new QCHelper();
helper.Section = bogyconfiglist.Section;
helper.Li = bogyconfiglist.LineItem;
helper.CrossCalc1Q = "";
helper.AllowComma = false;
helper.FormulaLiConfig = "";
helperlist.Add(helper);
}
stopwatch2.Stop();
Console.WriteLine(stopwatch1.ElapsedMilliseconds);
Console.WriteLine(stopwatch2.ElapsedMilliseconds);
Console.ReadKey();
}
}
Comparing result:
Hope my code example could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.