[SOLVED] If i Wanted to check the values of two columns against the current object how would i do it with linq to sql ? [modified]
-
var authors = (Author)this.bookBindingSource.Current; // get the authors object
using (var db = new BooksDataContext())
{
var OriginalAuthor = db.Authors.Where(af => af.AuthorFirst == authors.AuthorFirst)
db.Authors.Where(al => al.AuthorLast == authors.AuthorLast);if (originalAuthor != null) { originalAuthor.AuthorFirst = authors.AuthorFirst; originalAuthor.AuthorLast = authors.AuthorLast; } else { db.Authors.InsertOnSubmit(authors); } db.SubmitChanges(); } } {
****What I'm trying to do here is get an object pasted back to OriginalAuthor that contains the row where the AuthorsFirstname Column is equal to the current entity state's AuthorFirstname and also for the AuthorsLast compare the same. Then if it exist I want to simply ignore the update, otherwise I want to add it to the database... I'm confused on the whole double where clause :confused:
modified on Tuesday, February 2, 2010 4:23 PM
-
var authors = (Author)this.bookBindingSource.Current; // get the authors object
using (var db = new BooksDataContext())
{
var OriginalAuthor = db.Authors.Where(af => af.AuthorFirst == authors.AuthorFirst)
db.Authors.Where(al => al.AuthorLast == authors.AuthorLast);if (originalAuthor != null) { originalAuthor.AuthorFirst = authors.AuthorFirst; originalAuthor.AuthorLast = authors.AuthorLast; } else { db.Authors.InsertOnSubmit(authors); } db.SubmitChanges(); } } {
****What I'm trying to do here is get an object pasted back to OriginalAuthor that contains the row where the AuthorsFirstname Column is equal to the current entity state's AuthorFirstname and also for the AuthorsLast compare the same. Then if it exist I want to simply ignore the update, otherwise I want to add it to the database... I'm confused on the whole double where clause :confused:
modified on Tuesday, February 2, 2010 4:23 PM
Hi, You can join multiple conditions using && or ||. Also, I would prefer the use of Any method for this scenario:
bool exists = db.Authors.Any(af => af.AuthorFirst == authors.AuthorFirst && af.AuthorLast == authors.AuthorLast);
if (!exists) ...
Regards,
Syed Mehroz Alam My Blog | My Articles
Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein -
Hi, You can join multiple conditions using && or ||. Also, I would prefer the use of Any method for this scenario:
bool exists = db.Authors.Any(af => af.AuthorFirst == authors.AuthorFirst && af.AuthorLast == authors.AuthorLast);
if (!exists) ...
Regards,
Syed Mehroz Alam My Blog | My Articles
Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert EinsteinTHANKS that worked great!!