LINQ - Referential integrity - Asked By waseem kaleem on 04-Oct-11 04:41 AM

Suppose two tables
Category(id,name,description) parent table
book(id,name,catoryId) child table
I want to perform some checks in linq to sql
1)Only those record is stored in Child Table whose category id exists in Parent Table.
2)all those records of parent table which contain records in child are not deleted 
Mihir Soni replied to waseem kaleem on 04-Oct-11 04:43 AM
Hi,

You can consider using automapper to copy the values for you. Check this for more info:http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx


Hope this helps you

Thank you
Suchit shah replied to waseem kaleem on 04-Oct-11 05:06 AM
For those 2 requirement i would say just use Join which give u a common data which is avaliable in both the table that u can do it like below query

var test = from a in tableA
        Join b in tableB on a.Id = b.Id
        select {new A.Id,A.name,B.Id,B.test}
        order by A.Id
Chintan Vaghela replied to waseem kaleem on 12-Oct-11 03:31 AM
var query = from c in db.Category
           
join o in db.book
               
on c.id equals o.id into sr
           
from x in sr.DefaultIfEmpty()
           
select new {
               CategoryID
= c.id, Name=c.name,
               BookID
= x.id == null ? -1 : x.id};  
Sunil Darji replied to waseem kaleem on 25-Nov-11 09:53 AM
 
 try this query 

var Data = from a in book
Join b in Category on a.catoryId = b.id
select {new a.Id,a.name,a.catoryId}