public OptionValueSAP CompareOptionValues1(int id)
        {
            var modelitem = new OptionValueSAP { d_s_match = new List<DB_SAP_Match2>(), s_d_mismatch = new List<SAP_DB_Mismatch2>(), d_s_mismatch = new List<DB_SAP_Mismatch2>() };
            var sys = db.Lsystem.FirstOrDefault(x => x.LsystemID == id);
            var oplist = db.Option.Where(x => x.LsystemID == id).ToList();
            var ovlist = new List<OptionValue>();
            
            foreach(var item in db.FromSAP.Where(x=>x.MaterialNumber==sys.MaterialNumber).ToList())
            {
                bool match = false;
                bool s_d_mis = false;
                
                double ov_sap;
                bool flag = double.TryParse(item.OptionValue, out ov_sap);
                foreach(var op in oplist)
                {
                    foreach(var ov in op.OptionValues)
                    {
                        double ov_db;
                        bool flag_db = double.TryParse(ov.OptionVal, out ov_db);
                        if(flag&&flag_db && ov_db==ov_sap && op.OptionName==item.OptionName)
                        {
                            var p = new DB_SAP_Match2
                            {
                                DescriptionDB = ov.DescriptionEN,
                                DescriptionSAP = item.Description,
                                MaterialNo = item.MaterialNumber,
                                OptionID = op.OptionID,
                                OptionName = op.OptionName,
                                Optionval = ov.OptionVal,
                                OptionValID = ov.OptionValueID,
                                SystemID = id,
                                SystemName = sys.LsystemName
                            };

                            modelitem.d_s_match.Add(p);
                            match = true;
                            break;
                        }
                        else if(!flag&&!flag_db&& item.OptionValue==ov.OptionVal && op.OptionName==item.OptionName)
                        {
                            var p = new DB_SAP_Match2
                            {
                                DescriptionDB = ov.DescriptionEN,
                                DescriptionSAP = item.Description,
                                MaterialNo = item.MaterialNumber,
                                OptionID = op.OptionID,
                                OptionName = op.OptionName,
                                Optionval = ov.OptionVal,
                                OptionValID = ov.OptionValueID,
                                SystemID = id,
                                SystemName = sys.LsystemName
                            };


                            modelitem.d_s_match.Add(p);
                            match = true;
                            break;
                        }

                    }
                }
                if(!match)
                {
                    var p = new SAP_DB_Mismatch2
                    {
                        Description = item.Description,
                        MaterialNo = item.MaterialNumber,
                        OptionName = item.OptionName,
                        Optionval = item.OptionValue,
                        SystemID = id,
                        SystemName = sys.LsystemName
                    };
                    if (db.Option.Any(x => x.LsystemID == id && x.OptionName == item.OptionName))
                        p.OptionID = db.Option.FirstOrDefault(x => x.LsystemID == id && x.OptionName == item.OptionName).OptionID;
                    else
                        p.OptionID = 0;
                    modelitem.s_d_mismatch.Add(p);
                }
            }

                foreach(var op in db.Option.Where(x=>x.LsystemID==id))
                {
                    foreach(var ov in op.OptionValues)
                    {
                        //bool d_s_mis = false;
                        //foreach(var item in db.FromSAP.Where(x=>x.MaterialNumber==sys.MaterialNumber&& x.OptionName==op.OptionName))
                        //{
                        //    var p = new DB_SAP_Mismatch2
                        //    {
                        //        Description = ov.DescriptionEN,
                        //        MaterialNo = item.MaterialNumber,
                        //        OptionID = op.OptionID,
                        //        OptionName = op.OptionName,
                        //        Optionval = ov.OptionVal,
                        //        OptionValID = ov.OptionValueID,
                        //        SystemID = id,
                        //        SystemName = sys.LsystemName,
                        //    };
                        //    d_s_mis = true;
                        //    modelitem.d_s_mismatch.Add(p);
                        //    break;
                        //}
                        //if(!d_s_mis)
                        //{
                        //var p = new DB_SAP_Mismatch2
                        //{
                        //    Description = ov.DescriptionEN,
                        //    MaterialNo = sys.MaterialNumber,
                        //    OptionID = op.OptionID,
                        //    OptionName = op.OptionName,
                        //    Optionval = ov.OptionVal,
                        //    OptionValID = ov.OptionValueID,
                        //    SystemID = id,
                        //    SystemName = sys.LsystemName,
                        //};
                        //modelitem.d_s_mismatch.Add(p);
                        //}
                        
                        if(!modelitem.d_s_match.Any(x=>x.OptionName==op.OptionName&& x.Optionval==ov.OptionVal)&&!modelitem.s_d_mismatch.Any(x=>x.OptionName==op.OptionName&&x.Optionval==ov.OptionVal))
                        {
                            var p = new DB_SAP_Mismatch2
                            {
                                Description = ov.DescriptionEN,
                                MaterialNo = sys.MaterialNumber,
                                OptionID = op.OptionID,
                                OptionName = op.OptionName,
                                Optionval = ov.OptionVal,
                                OptionValID = ov.OptionValueID,
                                SystemID = id,
                                SystemName = sys.LsystemName,
                            };
                            modelitem.d_s_mismatch.Add(p);
                        }
                    }
                }
            
            return modelitem;
        }
 public ActionResult CompareOptionValues(int[] id)
 {
     var model = new List<OptionValueSAP>();
     var sap_materialno = db.FromSAP.Select(x => x.MaterialNumber).Distinct().ToList();
     var db_materialno = db.Lsystem.Select(x => x.MaterialNumber).Distinct().ToList();
     db_materialno.Remove("##########");
     if(id==null)
     {
         foreach(var item in sap_materialno.Intersect(db_materialno).ToList())
         {
             int lsysid = db.Lsystem.FirstOrDefault(x => x.MaterialNumber == item).LsystemID;
             var modelitem = new OptionValueSAP { d_s_match = new List<DB_SAP_Match2>(), d_s_mismatch = new List<DB_SAP_Mismatch2>(), s_d_mismatch = new List<SAP_DB_Mismatch2>() };
             modelitem = CompareOptionValues1(lsysid);
             model.Add(modelitem);
         }
     }
     else
     {
         foreach(var lsysid in id)
         {
             var modelitem = new OptionValueSAP { d_s_match = new List<DB_SAP_Match2>(), d_s_mismatch = new List<DB_SAP_Mismatch2>(), s_d_mismatch = new List<SAP_DB_Mismatch2>() };
             modelitem = CompareOptionValues1(lsysid);
             model.Add(modelitem);
         }
     }
     ViewBag.SystemFilter = db.Lsystem.Where(x => x.LsystemID != 1038).ToList();
     return View(model);
 }