LINQ]如何使用LINQ检查SQL Server中的项是否存在具有特定值的特定值? [英] LINQ]How to check if item in SQL Server exists with specific value in given column with LINQ?

查看:83
本文介绍了LINQ]如何使用LINQ检查SQL Server中的项是否存在具有特定值的特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我想检查我在LINQ中传递的数据是否已存在于数据库中。



这是我的代码



Hi
I want to check that the data which i'm passing in LINQ is already exist in database.

Here is my code

public void InsertChargesDetails()
       {
           UPLCL.Update();
           if (TXTSRFID.Text != "")
           {
               LblAlert.Text = "";
               if(TXTVolume.Text!="" && TXTLBuyrate.Text!="" && TXTLSellrate.Text!="" && TXTLRemark.Text!="" && UOMID.Text!="")
               {
                   using (LQTransSeaFreightDataContext DB = new LQTransSeaFreightDataContext())
                   {
                       var newLCL = new TB_TransSeaLCL
                       {
                           tSLC_tSEA_NUPKId = mobjGenlib.ConvertLong(TXTSRFID.Text),
                           tSLC_mUOM_NUPKId = mobjGenlib.ConvertLong(UOMID.Text),
                           tSLC_VCVolume = TXTVolume.Text,
                           tSLC_VCBuyRate = TXTLBuyrate.Text,
                           tSLC_VCSellRate = TXTLSellrate.Text,
                           tSLC_VCRemark = TXTLRemark.Text,
                           tSLC_NUIsActive = 1,
                           tSLC_mCMP_NUUniqueId = mobjGenlib.ConvertLong(TXTCompanyID.Text)
                       };
if (newLCL != null)
                       DB.TB_TransSeaLCLs.InsertOnSubmit(newLCL);
                       DB.SubmitChanges();
                       TB_TransSeaLCL MASACCMULWARR = DB.TB_TransSeaLCLs.OrderByDescending(C => C.tSLC_NUPKId).Where(A => A.tSLC_NUIsActive == 1 && A.tSLC_mCMP_NUUniqueId == mobjGenlib.ConvertLong(TXTCompanyID.Text)).FirstOrDefault();
                       //Insert In UserLog Report
                       mobjGenlib.InsertUserlog("tSLC", mobjGenlib.ConvertString(MASACCMULWARR.tSLC_NUPKId), "I");
                   }
               }
               else
               {
                   ScriptManager.RegisterStartupScript(this, UPLCL.GetType(), "ALERT", "alert('The Fields Should not be left blank');", true);
               }


           }
          }

       }





但它不起作用jst继续插入相同的数据,每当我点击保存。



有人请帮助我。



先谢谢。



But it is not working it jst keep on inserting the same data whenever i click save.

Someone please help me.

Thanks in Advance.

推荐答案

这是我如何克服上述问题的方式



Here is the way how i overcome from the above issue

public void InsertChargesDetails()
     {
         UPLCL.Update();
         if (TXTSRFID.Text != "")
         {
             LblAlert.Text = "";
             if(TXTVolume.Text!="" && TXTLBuyrate.Text!="" && TXTLSellrate.Text!="" && TXTLRemark.Text!="" && UOMID.Text!="")
             {
                 using (LQTransSeaFreightDataContext DB = new LQTransSeaFreightDataContext())
                 {
                     if ((from u in DB.TB_TransSeaLCLs where u.tSLC_VCVolume == TXTVolume.Text && u.tSLC_VCBuyRate == TXTLBuyrate.Text && u.tSLC_VCSellRate == TXTSellRate.Text && u.tSLC_VCRemark == TXTLRemark.Text && u.tSLC_mUOM_NUPKId == mobjGenlib.ConvertLong(UOMID.Text) && u.tSLC_NUIsActive == 1 select u).Count() > 0)
                     {
                         ScriptManager.RegisterStartupScript(this, UPLCL.GetType(), "ALERT", "alert('Already Exist');", true);
                     }
                     else
                     {
                         var newLCL = new TB_TransSeaLCL
                         {
                             tSLC_tSEA_NUPKId = mobjGenlib.ConvertLong(TXTSRFID.Text),
                             tSLC_mUOM_NUPKId = mobjGenlib.ConvertLong(UOMID.Text),
                             tSLC_VCVolume = TXTVolume.Text,
                             tSLC_VCBuyRate = TXTLBuyrate.Text,
                             tSLC_VCSellRate = TXTLSellrate.Text,
                             tSLC_VCRemark = TXTLRemark.Text,
                             tSLC_NUIsActive = 1,
                             tSLC_mCMP_NUUniqueId = mobjGenlib.ConvertLong(TXTCompanyID.Text)
                         };

                         DB.TB_TransSeaLCLs.InsertOnSubmit(newLCL);
                         DB.SubmitChanges();
                         TB_TransSeaLCL MASACCMULWARR = DB.TB_TransSeaLCLs.OrderByDescending(C => C.tSLC_NUPKId).Where(A => A.tSLC_NUIsActive == 1 && A.tSLC_mCMP_NUUniqueId == mobjGenlib.ConvertLong(TXTCompanyID.Text)).FirstOrDefault();
                         //Insert In UserLog Report
                         mobjGenlib.InsertUserlog("tSLC", mobjGenlib.ConvertString(MASACCMULWARR.tSLC_NUPKId), "I");

                     }
                 }
             }
             else
             {
                 ScriptManager.RegisterStartupScript(this, UPLCL.GetType(), "ALERT", "alert('The Fields Should not be left blank');", true);
             }


         }


     }


这篇关于LINQ]如何使用LINQ检查SQL Server中的项是否存在具有特定值的特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆