views:

42

answers:

1

hi, here is my scenario i got

Table1

  • id
  • name

Table2

  • id
  • family
  • fid

with one to many relationship set between Table1. id and Table2.fid

now here is my WCF service Code

 [OperationContract]

public List<Table1> GetCustomers(string numberToFetch)
{
    using (DataClassesDataContext context = new DataClassesDataContext())
    {
        return context.Table1s.Take(int.Parse(numberToFetch)).ToList( );
    }
}

and my ASPX page Code

<body xmlns:sys="javascript:Sys"   
    xmlns:dataview="javascript:Sys.UI.DataView">  
  <div id="CustomerView"   
      class="sys-template"  
      sys:attach="dataview"  
      dataview:autofetch="true"  
      dataview:dataprovider="Service2.svc"  
      dataview:fetchParameters="{{ {numberToFetch: 2} }}"  
      dataview:fetchoperation="GetCustomers">  
      <ul>  
          <li>{{family}}</li>  
      </ul>  
  </div> 

though i set serialization mode to Unidirectional in Linq2Sql designer i am not able to get the family value and all what i get is this in firebug

{"d":[{"__type":"Table1:#","id":1,"name":"asd"},{"__type":"Table1:#","id":2,"name":"wewe"}]}

any help would be totally appreciated

+1  A: 

Well, the point is: your method GetCustomers only ever selects from Table1 - I don't see any reference at all to Table2, where your Family column is located......

You need to check into Linq-to-SQL JOIN's and how to fetch data from a joined table into your result set.

Something along the lines of:

[DataContract]
class JoinedResult
{   
   [DataMember]
   public int Table1ID { get; set; }
   [DataMember]
   public string Table1Name { get; set; }
   [DataMember]
   public string Table2Family { get; set; }
}  

[OperationContract]
public List<JoinedResult> GetCustomers(int numberToFetch)
{
    using (DataClassesDataContext context = new DataClassesDataContext())
    {
        var q = from t1 in context.Table1
                join t2 in context.Table2 on t1.id = t2.fid
                select new JoinedResult
                           { Table1ID = t1.ID,
                             Table1Name = t1.Name,
                             Table2Family = t2.Family };

        return q.Take(numberToFetch).ToList();
    }
}

and btw: you should really make numberToFetch an INT parameter! Let the caller make the conversion......

UPDATE: if you don't want to explicitly include the second table to be queried, you could instead add a DataLoadOption to your context:

DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Table1>(t => t.Table2);

context.LoadOptions = dlo;

In that case, you tell LINQ to always include all elements from Table2 when it loads anything from Table1 - that should work, too.

marc_s
thanks alot for the answer, i may be wrong, but with Linq2sql mapping table2 is a property of table1 thus there is no need to join and i can access it like Table1.Table2, correct me if i am wrong please.
@devmania: yes, you could - but that doesn't mean it gets serialized back to the client. When you select *only* from Table1, then there's no inclination for Linq-to-SQL to include all Table2 properties as well. If you want to, you need to ask for it. But you're right - you could write: `Table2Family = t1.Table2.Family` as the last line in the select statement in LINQ
marc_s
bro thanks alot for your detailed answer, but the the thing is that i am creating a pluggable system so i want t a way to just pick up the new child tables without reediting my WCF code, so dataoption will not work because i have to reedit it every-time i add new plugin, this is really driving me crazy