Results 1 to 5 of 5

Thread: Linq to DataSet Left Join

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Linq to DataSet Left Join

    Hello

    Once again with a linq query problem...
    The query works but only retrieve the joined rows, i want to retrieve all rows in the first datatable.

    My current query:
    vb.net Code:
    1. Dim q = From regE In (From registoE In _dtRegistosAEnviar.AsEnumerable
    2.                Group registoE By colaborador = registoE.Field(Of String)("Colaborador") Into grupoE = Group
    3.                Select New With {
    4.                    .Colaborador = colaborador,
    5.                    .Seg = grupoE.Sum(Function(registoE) registoE.Field(Of Decimal)("Seg"))
    6.                }).AsEnumerable,
    7.                regS In (From registoS In _dtRegistosEnviados.AsEnumerable
    8.                Group registoS By colaborador = registoS.Field(Of String)("Colaborador") Into grupoS = Group
    9.                Select New With {
    10.                    .Colaborador = colaborador,
    11.                    .Seg = grupoS.Sum(Function(registoS) registoS.Field(Of Decimal)("Seg"))
    12.                }).AsEnumerable
    13.                Where (regE.Colaborador = regS.Colaborador)

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Linq to DataSet Left Join

    This was the first result when I Googled:

    http://www.codeguru.com/columns/vb/article.php/c15057
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Linq to DataSet Left Join

    Thanks JMC

    But that doesn't work, i already tested two different ways, the first one, that was similar to that one in Codeguru, the INTO clause after the Equals isn't recognized, i used this in here. Then i looked to another page at MSDN doesn't know which one and change the query to the one that i wrote...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Linq to DataSet Left Join

    Hmmm... I didn't, and don't right now, have the time to experiment and I've never done it before myself. I'll try later in the day if you still need it then.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Linq to DataSet Left Join

    Thanks but the join problem it's kind of solved.
    Now i have other problem, and i don't know what to do. In the selected rows of the join, if i put only the fields of the left table, the query works fine, but if i put the row that i need from the right table, when the table don't have values for the matching it raises a nullexception.

    I already tried to check if the value is nothing, but without luck.

    Here's the code:
    vb.net Code:
    1. Dim query = From regE In (From registoE In _dtRegistosAEnviar.AsEnumerable
    2.            Group registoE By colaborador = registoE.Field(Of String)("Colaborador") Into grupoE = Group
    3.            Select New With {
    4.                .Colaborador = colaborador,
    5.                .Seg = grupoE.Sum(Function(registoE) registoE.Field(Of Decimal)("Seg"))
    6.            }).AsEnumerable
    7.            Group Join regS In (From registoS In _dtRegistosEnviados.AsEnumerable
    8.            Group registoS By colaborador = registoS.Field(Of String)("Colaborador") Into grupoS = Group
    9.            Select New With {
    10.                .ColaboradorS = colaborador,
    11.                .SegS = grupoS.Sum(Function(registoS) registoS.Field(Of Decimal?)("Seg"))
    12.            }).AsEnumerable
    13.            On regE.Colaborador Equals regS.ColaboradorS Into juncao = Group
    14.             From ele In juncao.DefaultIfEmpty
    15.             Select New With {
    16.                .Colaborador = regE.Colaborador,
    17.                .HorasPorLancar = regE.Seg,
    18.                .TheErrorColum= ele.SegS <<<<<<<<<  ERROR >>>>>>>
    19.             }

    If i remove the line that throws the exception it works fine and i have all the values, but when i put this error column it throws the exception.

    I already tried this
    vb.net Code:
    1. IIf(ele.SegS Is Nothing, 0, ele.SegS)

    and this
    vb.net Code:
    1. If(ele.SegS Is Nothing, 0, ele.SegS)

    and this
    vb.net Code:
    1. IIf(IsDBNull(ele.SegS), 0, ele.SegS)

    and this
    vb.net Code:
    1. If(IsDBNull(ele.SegS), 0, ele.SegS)

    Rate People That Helped You
    Mark Thread Resolved When Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width