Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. alias names for Column in Linq

alias names for Column in Linq

Scheduled Pinned Locked Moved C#
helpcsharpdatabaselinqtutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    Hi I have a linq query in the following way where two column names from 2 different tables is same, which is giving me error, can somebody please help me in this? How to resolve with alias names if the names are same from 2 different tables and they are used either in select or in group - thank you so much in advance. Here is my linq query:

                    ViolationsUniquePenaltyApplied = (from a in violations
                                                      join b in programs on a.ViolationTypeId equals b.ViolationTypeId
                                                      join d in branches on b.BranchId equals d.BranchId
                                                      join e in programs on a.ViolationSubtypeId equals e.ViolationTypeId into es from e in es.DefaultIfEmpty()
                                                      where a.ApplyUniquePenalty == true
                                                      group new { a.ViolationSubtypeId, e.ViolationTypeName, a?.ViolationTypeId, b?.ViolationTypeName, b?.ParentViolationId, d?.BranchId, d?.Name, a.PenaltyAssessed, a.ViolationId, a.ViolationNumber }
                                                      by new { a?.ViolationTypeId, b?.ViolationTypeName, d?.BranchId, d?.Name, a.ViolationId }
                                        into g
                                                      select new
                                                      {
                                                          ViolationTypeId = g.FirstOrDefault().ViolationTypeId,
                                                          ViolationType = g.FirstOrDefault().ViolationTypeName,
                                                          ParentViolationType = (g.FirstOrDefault()?.ParentViolationId != null) ? (from e in programs where g.FirstOrDefault()?.ViolationTypeId == e.ParentViolationId select e.ViolationTypeName)?.FirstOrDefault() : g.FirstOrDefault().ViolationTypeName,
                                                          BranchId = g.FirstOrDefault().BranchId,
                                                          Branch = g.FirstOrDefault().Name,
                                                          PenaltyAssessed = g.Sum(pc => pc.PenaltyAssessed),
                                                          Cnt = g.Count(),
                                                          ViolationNumber = g.FirstOrDefault().ViolationNumber,
                                                          CountOrId
    
    L Richard DeemingR 2 Replies Last reply
    0
    • S simpledeveloper

      Hi I have a linq query in the following way where two column names from 2 different tables is same, which is giving me error, can somebody please help me in this? How to resolve with alias names if the names are same from 2 different tables and they are used either in select or in group - thank you so much in advance. Here is my linq query:

                      ViolationsUniquePenaltyApplied = (from a in violations
                                                        join b in programs on a.ViolationTypeId equals b.ViolationTypeId
                                                        join d in branches on b.BranchId equals d.BranchId
                                                        join e in programs on a.ViolationSubtypeId equals e.ViolationTypeId into es from e in es.DefaultIfEmpty()
                                                        where a.ApplyUniquePenalty == true
                                                        group new { a.ViolationSubtypeId, e.ViolationTypeName, a?.ViolationTypeId, b?.ViolationTypeName, b?.ParentViolationId, d?.BranchId, d?.Name, a.PenaltyAssessed, a.ViolationId, a.ViolationNumber }
                                                        by new { a?.ViolationTypeId, b?.ViolationTypeName, d?.BranchId, d?.Name, a.ViolationId }
                                          into g
                                                        select new
                                                        {
                                                            ViolationTypeId = g.FirstOrDefault().ViolationTypeId,
                                                            ViolationType = g.FirstOrDefault().ViolationTypeName,
                                                            ParentViolationType = (g.FirstOrDefault()?.ParentViolationId != null) ? (from e in programs where g.FirstOrDefault()?.ViolationTypeId == e.ParentViolationId select e.ViolationTypeName)?.FirstOrDefault() : g.FirstOrDefault().ViolationTypeName,
                                                            BranchId = g.FirstOrDefault().BranchId,
                                                            Branch = g.FirstOrDefault().Name,
                                                            PenaltyAssessed = g.Sum(pc => pc.PenaltyAssessed),
                                                            Cnt = g.Count(),
                                                            ViolationNumber = g.FirstOrDefault().ViolationNumber,
                                                            CountOrId
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What does the "simple" stand for?

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      1 Reply Last reply
      0
      • S simpledeveloper

        Hi I have a linq query in the following way where two column names from 2 different tables is same, which is giving me error, can somebody please help me in this? How to resolve with alias names if the names are same from 2 different tables and they are used either in select or in group - thank you so much in advance. Here is my linq query:

                        ViolationsUniquePenaltyApplied = (from a in violations
                                                          join b in programs on a.ViolationTypeId equals b.ViolationTypeId
                                                          join d in branches on b.BranchId equals d.BranchId
                                                          join e in programs on a.ViolationSubtypeId equals e.ViolationTypeId into es from e in es.DefaultIfEmpty()
                                                          where a.ApplyUniquePenalty == true
                                                          group new { a.ViolationSubtypeId, e.ViolationTypeName, a?.ViolationTypeId, b?.ViolationTypeName, b?.ParentViolationId, d?.BranchId, d?.Name, a.PenaltyAssessed, a.ViolationId, a.ViolationNumber }
                                                          by new { a?.ViolationTypeId, b?.ViolationTypeName, d?.BranchId, d?.Name, a.ViolationId }
                                            into g
                                                          select new
                                                          {
                                                              ViolationTypeId = g.FirstOrDefault().ViolationTypeId,
                                                              ViolationType = g.FirstOrDefault().ViolationTypeName,
                                                              ParentViolationType = (g.FirstOrDefault()?.ParentViolationId != null) ? (from e in programs where g.FirstOrDefault()?.ViolationTypeId == e.ParentViolationId select e.ViolationTypeName)?.FirstOrDefault() : g.FirstOrDefault().ViolationTypeName,
                                                              BranchId = g.FirstOrDefault().BranchId,
                                                              Branch = g.FirstOrDefault().Name,
                                                              PenaltyAssessed = g.Sum(pc => pc.PenaltyAssessed),
                                                              Cnt = g.Count(),
                                                              ViolationNumber = g.FirstOrDefault().ViolationNumber,
                                                              CountOrId
        
        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Assuming the problem is the two ViolationTypeName columns in your group statement - and assuming you actually need to group by both - you'll need to specify a different property name for one or both of them.

        group new
        {
        a.ViolationSubtypeId,
        e.ViolationTypeName,
        a?.ViolationTypeId,
        ViolationTypeName2 = b?.ViolationTypeName,
        b?.ParentViolationId,
        d?.BranchId,
        d?.Name,
        a.PenaltyAssessed,
        a.ViolationId,
        a.ViolationNumber
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups