How to change? RRS feed

  • Question

  • Use of EF, MYSql database

     DbQuery<AttenModel> dbQuery = db.AttenModels.Where(p => p.Id.ToString() == e.Node.Name).Take(1) as DbQuery<AttenModel>;
                    AttenModel model = dbQuery.FirstOrDefault();

    An error Will object reference not set to an instance of an object.

    But there is the record database, and don't know why no query? How to change?

    If query out first of all, in the screening can be found out

    please verify my account

    • Moved by CoolDadTx Friday, March 31, 2017 3:20 PM EF related
    Friday, March 31, 2017 5:20 AM


  • You have over complicated it, you can get the first record following way:

    AttenModel model = db.AttenModels.Where(p => p.Id.ToString() == e.Node.Name).FirstOrDefault();

    Hope It Helps!

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by lctk Saturday, May 27, 2017 11:38 PM
    Friday, March 31, 2017 9:18 AM