C# LINQ Expression needed

Experts,

I would like to have a C# LINQ Expression where I need to check whether a particular row already exists in the SQL Server table or not. Is there any efficient / faster way to achieve this.

I will be passing a string input as an input. I want the expression to return me a boolean value true or false.
SubbuUSAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubbuUSAAuthor Commented:
@Fernandosoto

Can you help me out here..
0
SubbuUSAAuthor Commented:
I used this code and ti works but I was thinking if there is any efficient way of doing this activity.

    try
                    {
                        SqlCommand RowExists = new SqlCommand("SELECT  1 As X WHERE EXISTS (Select 1 from [mast] where abc = '" + myval+ "')", connection);
                        recordExists = System.Convert.ToInt32(RowExists.ExecuteScalar());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.InnerException);
0
Fernando SotoRetiredCommented:
Hi SubbuUSA;

This should give you what you need.

var wasFound = DbContextInstance
                   .TableName
                   .Any(b => b.ColumnName == "InputStringValue");

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

SubbuUSAAuthor Commented:
@FernandoSoto

I did not understand how to pass value to this part (DbContextInstance)

After I wrote my code, it looks like this with a red on my_Mast .  string str = "data_pub";
  var wasFound = str.my_Mast.Any(b => b.cic == cic);

 compile  Error is thrown sayng

Error      2      'string' does not contain a definition for 'my_Mast' and no extension method 'my_Mast' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)      C:\Code\HFS.GSM.Paris\src\HFS.GSM.Paris.Flowthrough\GsmParisFlowthroughHandler.cs      137      44      HFS.GSM.Paris.Flowthrough
0
SubbuUSAAuthor Commented:
I think I need to use entity framework for that right?
0
Fernando SotoRetiredCommented:
Hi SubbuUSA;

I gave you a general example of a Linq to SQL/Linq to Entity Framework example that will return what you were looking for. If you do not have a Linq to SQL model or a Linq to EF model then that example will not work. My initial example with explanation.

var wasFound = DbContextInstance
                   .TableName
                   .Any(b => b.ColumnName == "InputStringValue");

Open in new window


Where DbContextInstance : is the name of your data context from the model.
           TableName : is the table name in the database you are querying
           ColumnName : is the name of the column in the table to match your input string.
           "InputStringValue" : to replace this string with the variable name holding the input.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SubbuUSAAuthor Commented:
@FernandoSoto

Ok got it. thanks
0
Fernando SotoRetiredCommented:
Not a problem, glad to be of help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.