public OptionSAP CompareOption1(int id)
 {
     var sys = db.Lsystem.FirstOrDefault(x => x.LsystemID == id);
     var modelitem = new OptionSAP { d_s_match = new List<DB_SAP_Match1>(), d_s_mismatch = new List<DB_SAP_Mismatch1>(), s_d_mismatch = new List<SAP_DB_Mismatch1>() };
     var db_oplist = db.Option.Where(x => x.LsystemID == id).Select(x=>x.OptionName).ToList();
     foreach(var item in db.FromSAP.Where(x=>x.MaterialNumber==sys.MaterialNumber).ToList())
     {
         if(db_oplist.Contains(item.OptionName))
         {
             if (!modelitem.d_s_match.Any(x => x.OptionName == item.OptionName))
             {
                 var match = new DB_SAP_Match1
                 {
                     LsystemID = id,
                     MaterialNo = sys.MaterialNumber,
                     OptionName = item.OptionName,
                     SystemName = sys.LsystemName,
                     OptionID = db.Option.FirstOrDefault(x => x.LsystemID == id && x.OptionName == item.OptionName).OptionID
                 };
                 modelitem.d_s_match.Add(match);
             }
         }
         else 
         {
             if (!modelitem.s_d_mismatch.Any(x => x.OptionName == item.OptionName))
             {
                 var mismatch = new SAP_DB_Mismatch1
                 {
                     LsystemID = id,
                     MaterialNo = sys.MaterialNumber,
                     OptionName = item.OptionName,
                     SystemName = sys.LsystemName
                 };
                 modelitem.s_d_mismatch.Add(mismatch);
             }
         }
             
     }
     foreach(var item in db_oplist)
     {
         if(!db.FromSAP.Where(x=>x.MaterialNumber==sys.MaterialNumber).Select(x=>x.OptionName).ToList().Contains(item) && !modelitem.d_s_mismatch.Any(x=>x.OptionName==item)&&!modelitem.d_s_match.Any(x=>x.OptionName==item))
         {
             var mismatch = new DB_SAP_Mismatch1
             {
                 LsystemID = id,
                 MaterialNo = sys.MaterialNumber,
                 OptionID = db.Option.FirstOrDefault(x => x.LsystemID == id && x.OptionName == item).OptionID,
                 OptionName = item,
                 SystemName = sys.LsystemName
             };
             modelitem.d_s_mismatch.Add(mismatch);
         }
     }
     return modelitem;
 }
 public ActionResult CompareOption(int[] id)
 {
     var model = new List<OptionSAP>();
     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())
         {
             var modelitem = new OptionSAP { d_s_match = new List<DB_SAP_Match1>(), d_s_mismatch = new List<DB_SAP_Mismatch1>(), s_d_mismatch = new List<SAP_DB_Mismatch1>() };
             int lsysid = db.Lsystem.FirstOrDefault(x => x.MaterialNumber == item).LsystemID;
             modelitem = CompareOption1(lsysid);
             model.Add(modelitem);
         }
     }
     else
     {
         foreach(var item in id)
         {
             var modelitem = new OptionSAP { d_s_match = new List<DB_SAP_Match1>(), d_s_mismatch = new List<DB_SAP_Mismatch1>(), s_d_mismatch = new List<SAP_DB_Mismatch1>() };                    
             modelitem = CompareOption1(item);
             model.Add(modelitem);
         }
     }
     ViewBag.SystemFilter = db.Lsystem.Where(x => x.LsystemID != 1038).ToList();
     
     return View(model);
 }