[RESOLVED]assign an ID from one table to another

I have a table game with id and another table scores related to table game as for every game there is score . table score have an attribute game_id.

I want in my controller page to set the game_id of the score table the value of id from the game table.

Here what I have tried so far

scoreBasket pl = db.scoreBaskets.Where(m => m.Id == scoreBasket.Id).FirstOrDefault();
if (pl != null)
                    {
                        var id = from p in db.games select (new { Id = p.Id });

                        pl.game_id = id.id;
                        pl.score=scoreBasket.score;
                     }

but there seem an error as the id in id.id doesn’t exist

can plaease some one help me with that

lolo512

var id = from p in db.games select (new { Id = p.Id });

THis is a list of Id’s . db.games are 1 row or more?

More, you want to use .ToList()

ignatandrei

More, you want to use .ToList()

yes ofcourse more than 1 row, and if I do 

pl.game_id = id;

it says that id id an IQueryable statement while pl.game_id is an int

lolo512

yes ofcourse more than 1 row

then  this

lolo512

 var id
= (from p
in db.games
select (new
{ Id
= p.Id
})).ToArray();

is an array of objects that contains id as property.

Which element from the array you want when you put 

lolo512

pl.game_id = id;

?

First Element? Last? Middle? None?

lolo512

var id = from p in db.games select (new { Id = p.Id });

This returns a list of id’s. Which particular id you want selected? Also, you can do it the same as your first linq query like:

pl.game_id = db.games.Where(g => g.Id == /*Id you want selected*/).Select(g => g.Id).FirstOrDefault();

 

Leave a Reply