views:

70

answers:

3

Here's my code

var bt = new BachtuocvnDataContext();
        var matchedTeams = (from lt in bt.Bet_Leagues_Teams
                         where lt.LeagueID == leagueID
                         select (lt)).Single();
        matchedTeams.TeamID = teamID;
        bt.SubmitChanges(ConflictMode.ContinueOnConflict);

It does not update the table. Traditional query works well, I found a similar question here:

http://stackoverflow.com/questions/206532/linq-not-updating-on-submitchanges

but I checked and found that Bet_Leagues_Teams does have a primary key.

Bet_Leagues_Teams class:

int ID (primary key)
int LeagueID;
int TeamID;

Ahhhh, MY TERRIBLE MISTAKE. I forgot that Bet_Leagues_Teams may not contains the record needed. I must check if the record existed, and then update it, or it does not exist and I must add it to the table. Shame on me. Forgive me for wasting your time. Thank you.

A: 
using( var bt = new BachtuocvnDataContext() )
{
    var matchedTeam = bt.Bet_Leagues_Teams.Single( lt => lt.LeagueID == leagueID );
    matchedTeam.TeamID = teamID;
    bt.SubmitChanges( ConflictMode.ContinueOnClonflict );
}
Jerod Houghtelling
it does not work ...
Vimvq1987
Sorry, its essentially the same as your example after your edit.
Jerod Houghtelling
+1  A: 
using(BachtuocvnDataContext bt = new BachtuocvnDataContext() ) 
{ 
    Bet_Leagues_Teams matchedTeam = 
        bt.Bet_Leagues_Teams.Where(lt => lt.LeagueID == leagueID)
        .SingleOrDefault(); 

    if(matchedTeam != null)
    {
        matchedTeam.TeamID = teamID; 
        bt.SubmitChanges(ConflictMode.ContinueOnClonflict); 
    }
} 
Andy Evans
A: 

Note that Single() will throw an exception if there is more than one matching elements (which, if your schemea models the concept of "league" properly, there will be)

You may want to use First() there.

James Curran