locked
join linq return datatype RRS feed

  • Question

  • if i want to join 2 tables then what type of list do i declare? i cant say a type of table because no table exists with same data and i cant make a class with properties because it wont pick up the class unless there is another way?

     

         <OperationContract()> _
           Function filldata3(ByVal sname As String) As List(Of ???? what do i have
            Dim db As New linq1.Web.DataClasses1DataContext

            Dim results = From p In db.subjects Join o In db.students _
                            On o.id Equals p.id _
                            Select p.id, p.subject, p.test, p.comment, o.name

            Return results.ToList


        End Function

    Sunday, November 16, 2008 7:01 AM

Answers

  • Yes you can but that is not good practiceBig Smile.

        <OperationContract()> _
        Function filldata3() As List(Of StudentInfo)
            Dim db As New DataClasses1DataContext

            Dim results As IEnumerable(Of StudentInfo) = _
            From p In db.subjects Join o In db.students _
            On o.id Equals p.id _
            Select p.id, p.Subject, p.test, p.Comment, o.name()

            Dim MyList As List(Of StudentInfo) = New List(Of StudentInfo)

            For Each res In results
                MyList.Add(New StudentInfo With {.id = res.id, .subject = res.subject, .test = res.test, .comment = res.comment, .name = res.name})
            Next

            Return
    MyList

        End Function

     

    Hope this will help you.

     

    Please Mark as Answer if this helps you.

    Amyo Kabir

    Solution Architect & Sr. Developer

    http://amyotech.spaces.live.com/

    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.

    Monday, November 17, 2008 3:19 PM

All replies

  • It's actually returning an anonymous type.

    Check how to work with anonymous type here: http://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=true

    Moreover for Silverlight WCF service you can create a new class with required property (DataContract) and the use it as:

    select new MyDataContract {ID=Select p.id, Subject=p.subject, Test= p.test, Comment= p.comment, o.name}

    Then you will get list of MyDataContract for WCF.

     

    Sunday, November 16, 2008 8:13 AM
  • select new MyDataContract {ID=Select p.id, Subject=p.subject, Test= p.test, Comment= p.comment, o.name}

    1)mydatacontract is a class i make with relevant fields to be returned.?

    2)could i just not iterate through results and then assing this to a class and return the list of this?

    Sunday, November 16, 2008 5:11 PM
  •   1) Yes MyDataContract is a class with your relevant fields.

    2)Yes you can iterate to populate the list (you can do it using the same LINQ query that you showed, just add select new MyDataContract{...)

    Moreover if you want to use MyDataContract in WCF service then:

    [DataContract]

    public class MyDataContract{

     [DataMember]

     public int Id{get;set;}

     ......

    }

    Sunday, November 16, 2008 11:28 PM
  •  i cant say a type of table because no table exists with same data and i cant make a class with properties because it wont pick up the class unless there is another way?

    Hi, please see this link, it would solve everytging related with wcf service, passing return data, to an silverlight application using [DataContract], [DataMember]and as this is a video, you cannot make any mistake.

    http://silverlight.net/learn/learnvideo.aspx?video=47177

    and if this post was helpful then please 'Mark as Answer' - many thanks

    Sharker Khaleed Mahmud
    Software Developer
    (MCP,MCTS,MCPD[web])

    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.

    Sunday, November 16, 2008 11:33 PM
  • Thats the Query:

    From p In db.subjects Join o In db.students _
                            On o.id Equals p.id _
    Select New MyDataContract With {.ID= p.id, .Subject=p.subject, .Test= p.test, .Comment= p.comment, .Name=o.name}

     

    Sunday, November 16, 2008 11:45 PM
  • is there a way to iterate with results and store in a class as well for interest sake?

     

        <OperationContract()> _
           Function filldata3() As List(Of StudentInfo)

            Dim db As New linq1.Web.DataClasses1DataContext

            Dim results As IEnumerable(Of StudentInfo) = _
           From p In db.subjects Join o In db.students _
           On o.id Equals p.id _
           Select New StudentInfo With {.id = p.id, .subject = p.subject, .test = p.test, .comment = p.comment, .name = o.name}

            Return results.ToList



        End Function

        Public Class StudentInfo

            Public id As String
            Public subject As String
            Public test As String
            Public comment As String
            Public name As String
        End Class

    End Class

     

     

    Monday, November 17, 2008 3:37 AM
  • Yes you can but that is not good practiceBig Smile.

        <OperationContract()> _
        Function filldata3() As List(Of StudentInfo)
            Dim db As New DataClasses1DataContext

            Dim results As IEnumerable(Of StudentInfo) = _
            From p In db.subjects Join o In db.students _
            On o.id Equals p.id _
            Select p.id, p.Subject, p.test, p.Comment, o.name()

            Dim MyList As List(Of StudentInfo) = New List(Of StudentInfo)

            For Each res In results
                MyList.Add(New StudentInfo With {.id = res.id, .subject = res.subject, .test = res.test, .comment = res.comment, .name = res.name})
            Next

            Return
    MyList

        End Function

     

    Hope this will help you.

     

    Please Mark as Answer if this helps you.

    Amyo Kabir

    Solution Architect & Sr. Developer

    http://amyotech.spaces.live.com/

    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.

    Monday, November 17, 2008 3:19 PM