Clive
Clive

Reputation: 13

Need to match a value from another table in c#

I have a table called HoldingBucket and i need to be able to look up to see if an Account name matches one entered in a request table when a class called initalcheck is running in my project.

in short, if value is present in holdingbucket i will code it to do something otherwise the code will continue to run

if request.accountname = (a record in the holding bucket) then .....

but i dont know how to get to a match

Sorry new to this and trying to learn and not seeming to fins an answer on this anywhere that i can apply

Started with this but guessing its way off

var InBucket = (from HB in _context.HoldingBucket
                where HB.AccountName == Request.Account_Name
                select HB);


if (InBucket != null)
{
    //do something
}

Upvotes: 0

Views: 626

Answers (1)

Grizzly
Grizzly

Reputation: 5953

Because you're performing this check in a separate class and not a Controller you could do something like this:

using System.Linq;

var isAMatch = _context.HoldingBucket.Any(x => x.AccountName.ToLower() == Request.Account_Name.ToLower());

// _context is what you provided me in the comments as your connection string variable

// isAMatch will either be true or false

if(isAMatch) // if there is a match
    doSomething();
else // if there is no match
    doSomethingElse();

This is just a simple way, but dependency injection is preferred.

If you are using Entity Framework, then you should use the LINQ method called Any. This method returns a boolean value indicating if a condition is met when dealing with a collection of objects. Basically, the above line is saying "If ANY AccountName in the HoldingBucket table matches request.AccountName then return true, otherwise return false"

Information on Any

Let me know if this helps.

Upvotes: 1

Related Questions