locked
Execute query to return entity object RRS feed

  • Question

  • User772484728 posted

    If my model creates a BANK objects from the BANK table, how can I execute a query to return a single bank?

    Why doesn't this work?

    BANK bank = db.BANKS.SqlQuery("SELECT * FROM BANKS WHERE BANK_NAME = @name", new SqlParameter("@name", bankName));

    Wednesday, December 16, 2015 9:40 PM

Answers

  • User-271186128 posted

    Hi CCPizza,

    If my model creates a BANK objects from the BANK table, how can I execute a query to return a single bank?

    Why doesn't this work?

    BANK bank = db.BANKS.SqlQuery("SELECT * FROM BANKS WHERE BANK_NAME = @name", new SqlParameter("@name", bankName));

    As for this issue, perhaps the above query statement returns multiple records, so, you could try to use the First() or FirstOrDefault() method. As below:

    BANK bank = db.BANKS.SqlQuery("SELECT * FROM BANKS WHERE BANK_NAME = @name", new SqlParameter("@name", bankName)).FirstOrDefault();

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 17, 2015 1:39 AM