How do I create a data relation on generic lists?

Posted on 2009-12-16
Last Modified: 2013-11-25
I have two generic lists that I wish to perform a data relation on similar to what you can do with data tables. One list is a list of parent objects, and the other is a list of child objects. Is this possible, and if so, how can it be done? I will be binding to an Infragistics UlgraGrid.

Environment: XP OS using VS 2008 .NET v3.5
Dim lstParent As New List(Of ParentObject)
lstParent = GetParentList(obj) 
Dim lstChild As New List(Of ChildObject)
lstChild = GetChildList(obj) 
Dim MyRelation as new DataRelation()

Open in new window

Question by:CADOIM
    LVL 15

    Expert Comment

    Your informaction is difuse.
    I know than you can create a datarelation Datatable.Colummn -  RelatatedTable.(keycolumn)
    also with Linq you can create a Lookup from a list to other enumerable (Net 3.5).
    but you neeed more explicit on requiriments.

    Sorry on gramatic, I`m spanish.
    Dataview only supports information on table columns,  lookup relate elementens on a colection on other.
    Linq result can be a datasource, also a dataview or datatable can be a datasource.
    I prefer datatable/datasource.
    But  wat you is intend?
    Datatable/dataview  has a lot of interfaces to use, BusinesObjects use Reflection to access public properties.

    I has expoxed on previous questions how to Linq can become an choice to classic DataView.

    I think Ilookup is the solution for you, bu it is a complex solution to explain with your exposition.
    Ilookup is a HasTable that contains a key from primary colletion that contains many elements on secondary collection.
    You need define a function to use with ToLookup of Linq.

            Public Shared Function GetSuc(ByVal c As CTE) As Integer
                Return c._Suc
            End Function
            LookupPred = Ctes.ToLookup(Of Int32)(AddressOf CTE.GetSuc)

    Where Ctes is a Ienumerable of Cte and Cte.GetSuc return a Int32.

    Code Result:

    Module Module1
        Sub Main()
            Dim list2 As New List(Of datos)
            list2.Add(New datos("a", "AValue1"))
            list2.Add(New datos("a", "AValue2"))
            list2.Add(New datos("b", "bValue1"))
            Dim MyLookup = (From Item In list2 Select Item).ToLookup(Of String)(AddressOf datos.getKey)
            For Each dato In MyLookup("a")
        End Sub
    End Module
    Public Class datos
        Public Key As String
        Public value As String
        Public Sub New(ByVal key As String, ByVal value As String)
            Me.Key = key
            Me.value = value
        End Sub
        Public Shared Function getKey(ByVal item As datos) As String
            Return item.Key
        End Function
    End Class

    Open in new window


    Author Comment


    Thank you for the quick reply. I really need to be able to pass in a list and have it do the conversion without having to explicitly write out each column. Also, we don't use Linq in our department to access the database.

    Here is what I ended up doing: I don't know how it works, but somehow by sending the list into an XML serializer, it can spit it out as a dataset (mind you it has difficulty returning a datatable). I can pass in the lists I want into this function, return them as datatables, combine them all back into one dataset, and create the relation on the dataset. Don't know if there's a better way, but this way works for me.
    LVL 15

    Expert Comment

    I worked only with Xml DataSet/DataTables or Config Settings, do´nt use XmlDocument .
    It´s easy, you create a  DataTable and fill it reading each row (Element) from Xml Document.
    Then bind DataTable to Infragistics UlgraGrid.

    Create DataTable:
    Dim MyTable as new DataTable
    MyTable.Columns.add(new Datacolumn("ColumnName", GetType(ColumnType))
    --- For each Column, Where ColumnType is String, Int32....
    For Each Document Row:

    To Work with XmlDocument :

    Accepted Solution

    My code must not have attached with my reply, let's try this again. Anyway, I didn't even have to manually add columns to my data table. The dataset can read the xml file and create its own columns. You then convert the data set to a data table for each list and combine all data tables into one data set to do the filtering on.

    Here is the link for more information:

    Not sure how to award points, since I used my own solution. Hopefully this will help someone else out though.
    Public Function ConvListToDataTable(ByVal lstValue As List(Of Object)) As DataTable
    Dim sb As New StringBuilder()
    Dim xmlSerializer As New XmlSerializer(GetType(List(Of Object)))
    Dim sw As New StringWriter(sb)
    xmlSerializer.Serialize(sw, lstValue)
    Dim stream As New StringReader(sb.ToString())
    Dim dSet As New DataSet
    Dim dt As New DataTable
    dt = dSet.Tables(0)
    Return dt
    End Function

    Open in new window

    LVL 15

    Expert Comment

    Microsoft Serialization is a typed model, if you serialize an object then you deserialize that object not other.
    This is enforced, you can´nt use DataSet.ReadXml from a stream that was created from serialization on an no dataset object.

    You need create a function that returns a DataRow from each object instance in the list.
    Then add each row to DataTable.

    Note, for fast DataTable creation, assign MyDataTable.MinimumCapacity = MyList.Count before add first row.
    Also use    BeginLoadData()   and  EndLoadData() to avoid raise events on each added DataRow.

    Author Comment

    Actually, you can. The code I posted above works, so evidently you can use DataSet.ReadXml to read a stream created from serialization on a list object. Don't ask me how it works, it just does 8)

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now