Ali Rezaii
Ali Rezaii

Reputation: 27

Access to the return value from the database by linq

I need to send a value to database this my code

var concerthallID = from _concert in db.tbl_Content_Context
    join _concerthall in db.tbl_Concert_ConcertHall on _concert.ContextID equals _concerthall.ContextID
    where _concert.EnContextID == concertid
    select _concerthall.ConcertHallID;

how to set ConcertHallID to a Variable?

Upvotes: 0

Views: 55

Answers (2)

oqx
oqx

Reputation: 2377

You'll need to select a single concert and select the id from it after making sure it is not null, the code should be something like this:

var concert = (from _concert in db.tbl_Content_Context
    join _concerthall in db.tbl_Concert_ConcertHall on _concert.ContextID equals _concerthall.ContextID
    where _concert.EnContextID == concertid
    select _concert).SingleOrDefault();

var concerthallID = 0;
if (concert !=null)
    concerthallID = concert.ConcertHallID;

Remember to surround this with a try catch block to make sure that the single is returning only one.

Upvotes: 1

Aram
Aram

Reputation: 5705

Your query will return an Iqueryable, so if it will return one row or you need the first one use this after your code:

var ID = concerthallID.FirstOrDefault();

Upvotes: 1

Related Questions