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
  1. Home
  2. The Lounge
  3. LINQ is fun!

LINQ is fun!

Scheduled Pinned Locked Moved The Lounge
csharpdatabaselinqcom
29 Posts 21 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.
  • M Marc Clifton

    Especially when combined with extension methods and fluent-style methods.

    envvars
    .Municipalities()
    .Select(m => m.Fixups())
    .NotServiceAccess()
    .Where(m => m.VerifyConnection(_ => Log($"{m.Municipality}: {m.ConnectionString} => Connection failed.")))
    .Do(munis =>
    munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.NotIn(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
    .ForEach(q => q
    .Iterate((t, m) =>
    t
    .Do(_ => Log($"{m.Municipality}: {t.TableName} => Audit table missing"))
    .GenerateMissingAuditTableSql(m)
    .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\r\nFailed to execute SQL: {sql.ToString()}"))
    )))
    .Do(munis =>
    {
    munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.In(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
    .ForEach(q => q
    .Iterate((t, m) =>
    {
    Log($"{m.Municipality}: {t.TableName} => Checking...");
    var entityColumns = M5.GetColumnList(DBO_SCHEMA, m.ConnectionString, t.TableName);
    var auditColumns = M5.GetColumnList(AUDIT_SCHEMA, m.ConnectionString, t.TableName);

                    entityColumns.NotIn(auditColumns, s => s.Name.ToLower(), f => f.Name.ToLower())
                        .DoIf(q2 => q2.Count() > 0, q3 => Log($"  {q3.Count()} missing fields"))
                        .ForEach(f =>
                        {
                            Log($"  {m.Municipality}: {t.TableName} => Field {f} missing in audit table");
    
                            f
                                .GenerateMissingColumnSql(m, AUDIT\_SCHEMA, t.TableName)
                                .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\\r\\nFailed to execute SQL: {sql.ToString()}"));
                        });
                }));
        });
    

    This is a custom "one line" function that creates missing audit tables or adds the missing columns that the entity defines but someone forgot to add to the audit table. :-D

    Latest Articles:
    Client-Side Type-Based Publish

    J Offline
    J Offline
    jarvisa
    wrote on last edited by
    #19

    Strictly speaking, this is not LINQ. LINQ stands for Language-Integrated Query and is a query language that can be embedded within C#. You have written C# code using the classes that support LINQ. As an exercise, try rewriting your code using LINQ.

    1 Reply Last reply
    0
    • M Marc Clifton

      Especially when combined with extension methods and fluent-style methods.

      envvars
      .Municipalities()
      .Select(m => m.Fixups())
      .NotServiceAccess()
      .Where(m => m.VerifyConnection(_ => Log($"{m.Municipality}: {m.ConnectionString} => Connection failed.")))
      .Do(munis =>
      munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.NotIn(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
      .ForEach(q => q
      .Iterate((t, m) =>
      t
      .Do(_ => Log($"{m.Municipality}: {t.TableName} => Audit table missing"))
      .GenerateMissingAuditTableSql(m)
      .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\r\nFailed to execute SQL: {sql.ToString()}"))
      )))
      .Do(munis =>
      {
      munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.In(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
      .ForEach(q => q
      .Iterate((t, m) =>
      {
      Log($"{m.Municipality}: {t.TableName} => Checking...");
      var entityColumns = M5.GetColumnList(DBO_SCHEMA, m.ConnectionString, t.TableName);
      var auditColumns = M5.GetColumnList(AUDIT_SCHEMA, m.ConnectionString, t.TableName);

                      entityColumns.NotIn(auditColumns, s => s.Name.ToLower(), f => f.Name.ToLower())
                          .DoIf(q2 => q2.Count() > 0, q3 => Log($"  {q3.Count()} missing fields"))
                          .ForEach(f =>
                          {
                              Log($"  {m.Municipality}: {t.TableName} => Field {f} missing in audit table");
      
                              f
                                  .GenerateMissingColumnSql(m, AUDIT\_SCHEMA, t.TableName)
                                  .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\\r\\nFailed to execute SQL: {sql.ToString()}"));
                          });
                  }));
          });
      

      This is a custom "one line" function that creates missing audit tables or adds the missing columns that the entity defines but someone forgot to add to the audit table. :-D

      Latest Articles:
      Client-Side Type-Based Publish

      A Offline
      A Offline
      Andrey Logunov
      wrote on last edited by
      #20

      Still too many ";"

      1 Reply Last reply
      0
      • M Maximilien

        (serious) Is there a reason to write ugly code like that ? Is it more efficient ? If I was looking at code like that I would probably send it back after code review. :confused:

        I'd rather be phishing!

        O Offline
        O Offline
        obermd
        wrote on last edited by
        #21

        Chances are it's less efficient because of the overhead inherit in LINQ itself.

        J 1 Reply Last reply
        0
        • M Marc Clifton

          Especially when combined with extension methods and fluent-style methods.

          envvars
          .Municipalities()
          .Select(m => m.Fixups())
          .NotServiceAccess()
          .Where(m => m.VerifyConnection(_ => Log($"{m.Municipality}: {m.ConnectionString} => Connection failed.")))
          .Do(munis =>
          munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.NotIn(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
          .ForEach(q => q
          .Iterate((t, m) =>
          t
          .Do(_ => Log($"{m.Municipality}: {t.TableName} => Audit table missing"))
          .GenerateMissingAuditTableSql(m)
          .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\r\nFailed to execute SQL: {sql.ToString()}"))
          )))
          .Do(munis =>
          {
          munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.In(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
          .ForEach(q => q
          .Iterate((t, m) =>
          {
          Log($"{m.Municipality}: {t.TableName} => Checking...");
          var entityColumns = M5.GetColumnList(DBO_SCHEMA, m.ConnectionString, t.TableName);
          var auditColumns = M5.GetColumnList(AUDIT_SCHEMA, m.ConnectionString, t.TableName);

                          entityColumns.NotIn(auditColumns, s => s.Name.ToLower(), f => f.Name.ToLower())
                              .DoIf(q2 => q2.Count() > 0, q3 => Log($"  {q3.Count()} missing fields"))
                              .ForEach(f =>
                              {
                                  Log($"  {m.Municipality}: {t.TableName} => Field {f} missing in audit table");
          
                                  f
                                      .GenerateMissingColumnSql(m, AUDIT\_SCHEMA, t.TableName)
                                      .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\\r\\nFailed to execute SQL: {sql.ToString()}"));
                              });
                      }));
              });
          

          This is a custom "one line" function that creates missing audit tables or adds the missing columns that the entity defines but someone forgot to add to the audit table. :-D

          Latest Articles:
          Client-Side Type-Based Publish

          B Offline
          B Offline
          Bruce Patin
          wrote on last edited by
          #22

          There is a module for using actual, real SQL statements in code, rather than LINQ. I had to use it once for ad hoc sorting, because LINQ was way too cumbersome. Too bad the LINQ creators couldn't incorporate it.

          J 1 Reply Last reply
          0
          • M Marc Clifton

            Maximilien wrote:

            Is there a reason to write ugly code like that ?

            I was curious to what extent, ok, extreme, I could go. One of the things I love about functional programming is the pipe operators |> and <| which have no equivalent in C#, the best one can do is the dot operator. So I wanted to play with the concept, see how far I could take it, what extension methods I needed to create to maintain the "continuation", how hard it would be to debug, and if the result looked in any way maintainable. One of the things I discovered is that, as usual, wrapping simple things like "where" expressions into a named extension method improves readability but potentially decreases the "well, how is that actually accomplished" immediate comprehension, and naming can be quite difficult when creating bizarre functions that iterate over a collection and pass in both the collection item and some other pre-computed value. So consider it a "thought experiment" in actual implementation. ;)

            Latest Articles:
            Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #23

            IMO the biggest problem with overly complex linq is that it's all one statement to the debugger. If you to step from one . to the next being able to view the data being passed around without having to spam replace each . with ;\r\n foo = and turning anonymous functions into normal ones (and of course undoing all of that when done) troubleshooting would be much easier.

            Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

            1 Reply Last reply
            0
            • D den2k88

              I read it aloud and accidentally summoned and eldritch horror. Please provide the unsummon chant.

              GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #24

              say:

              oh
              wa
              tagu
              sigh
              am

              ten times as fast as you can. if that doesn't work, try

              me
              we
              todd
              ed

              instead. /trollface

              Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

              1 Reply Last reply
              0
              • O obermd

                Chances are it's less efficient because of the overhead inherit in LINQ itself.

                J Offline
                J Offline
                James Curran
                wrote on last edited by
                #25

                There is no overhead "inherit" in LINQ. There is some in the current JIT implementation of lambdas, but a better JITter could inline them, which would make them more efficient than functions.

                Truth, James

                1 Reply Last reply
                0
                • B Bruce Patin

                  There is a module for using actual, real SQL statements in code, rather than LINQ. I had to use it once for ad hoc sorting, because LINQ was way too cumbersome. Too bad the LINQ creators couldn't incorporate it.

                  J Offline
                  J Offline
                  James Curran
                  wrote on last edited by
                  #26

                  Huh? I'm very confused by that statement. Are you attempting to sort a database table/query or an in-memory collection? Either way, I don't see how a proper SQL syntax would be any less "cumbersome" than the LINQ syntax.

                  Truth, James

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    Especially when combined with extension methods and fluent-style methods.

                    envvars
                    .Municipalities()
                    .Select(m => m.Fixups())
                    .NotServiceAccess()
                    .Where(m => m.VerifyConnection(_ => Log($"{m.Municipality}: {m.ConnectionString} => Connection failed.")))
                    .Do(munis =>
                    munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.NotIn(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
                    .ForEach(q => q
                    .Iterate((t, m) =>
                    t
                    .Do(_ => Log($"{m.Municipality}: {t.TableName} => Audit table missing"))
                    .GenerateMissingAuditTableSql(m)
                    .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\r\nFailed to execute SQL: {sql.ToString()}"))
                    )))
                    .Do(munis =>
                    {
                    munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.In(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
                    .ForEach(q => q
                    .Iterate((t, m) =>
                    {
                    Log($"{m.Municipality}: {t.TableName} => Checking...");
                    var entityColumns = M5.GetColumnList(DBO_SCHEMA, m.ConnectionString, t.TableName);
                    var auditColumns = M5.GetColumnList(AUDIT_SCHEMA, m.ConnectionString, t.TableName);

                                    entityColumns.NotIn(auditColumns, s => s.Name.ToLower(), f => f.Name.ToLower())
                                        .DoIf(q2 => q2.Count() > 0, q3 => Log($"  {q3.Count()} missing fields"))
                                        .ForEach(f =>
                                        {
                                            Log($"  {m.Municipality}: {t.TableName} => Field {f} missing in audit table");
                    
                                            f
                                                .GenerateMissingColumnSql(m, AUDIT\_SCHEMA, t.TableName)
                                                .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\\r\\nFailed to execute SQL: {sql.ToString()}"));
                                        });
                                }));
                        });
                    

                    This is a custom "one line" function that creates missing audit tables or adds the missing columns that the entity defines but someone forgot to add to the audit table. :-D

                    Latest Articles:
                    Client-Side Type-Based Publish

                    M Offline
                    M Offline
                    MSBassSinger
                    wrote on last edited by
                    #27

                    The more they complicate the plumbing, the easier it is to stop up the drain. One look at that, and I would start asking: 1. "What other ways can I do the same thing" 2. "Of those other ways, which provides the most value over the full SDLC?"

                    1 Reply Last reply
                    0
                    • M Marc Clifton

                      Especially when combined with extension methods and fluent-style methods.

                      envvars
                      .Municipalities()
                      .Select(m => m.Fixups())
                      .NotServiceAccess()
                      .Where(m => m.VerifyConnection(_ => Log($"{m.Municipality}: {m.ConnectionString} => Connection failed.")))
                      .Do(munis =>
                      munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.NotIn(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
                      .ForEach(q => q
                      .Iterate((t, m) =>
                      t
                      .Do(_ => Log($"{m.Municipality}: {t.TableName} => Audit table missing"))
                      .GenerateMissingAuditTableSql(m)
                      .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\r\nFailed to execute SQL: {sql.ToString()}"))
                      )))
                      .Do(munis =>
                      {
                      munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.In(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
                      .ForEach(q => q
                      .Iterate((t, m) =>
                      {
                      Log($"{m.Municipality}: {t.TableName} => Checking...");
                      var entityColumns = M5.GetColumnList(DBO_SCHEMA, m.ConnectionString, t.TableName);
                      var auditColumns = M5.GetColumnList(AUDIT_SCHEMA, m.ConnectionString, t.TableName);

                                      entityColumns.NotIn(auditColumns, s => s.Name.ToLower(), f => f.Name.ToLower())
                                          .DoIf(q2 => q2.Count() > 0, q3 => Log($"  {q3.Count()} missing fields"))
                                          .ForEach(f =>
                                          {
                                              Log($"  {m.Municipality}: {t.TableName} => Field {f} missing in audit table");
                      
                                              f
                                                  .GenerateMissingColumnSql(m, AUDIT\_SCHEMA, t.TableName)
                                                  .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\\r\\nFailed to execute SQL: {sql.ToString()}"));
                                          });
                                  }));
                          });
                      

                      This is a custom "one line" function that creates missing audit tables or adds the missing columns that the entity defines but someone forgot to add to the audit table. :-D

                      Latest Articles:
                      Client-Side Type-Based Publish

                      M Offline
                      M Offline
                      MadGerbil
                      wrote on last edited by
                      #28

                      I appreciate the power of LINQ. I also love how Entity Framework can use the power of LINQ to turn a simple SQL query into a multiple library, dozen of files, partial files and nonsense cluster elephant. I hate everyone because of this.

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        Especially when combined with extension methods and fluent-style methods.

                        envvars
                        .Municipalities()
                        .Select(m => m.Fixups())
                        .NotServiceAccess()
                        .Where(m => m.VerifyConnection(_ => Log($"{m.Municipality}: {m.ConnectionString} => Connection failed.")))
                        .Do(munis =>
                        munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.NotIn(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
                        .ForEach(q => q
                        .Iterate((t, m) =>
                        t
                        .Do(_ => Log($"{m.Municipality}: {t.TableName} => Audit table missing"))
                        .GenerateMissingAuditTableSql(m)
                        .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\r\nFailed to execute SQL: {sql.ToString()}"))
                        )))
                        .Do(munis =>
                        {
                        munis.SelectWith(m => M5.GetTableNames(AUDIT_SCHEMA, m.ConnectionString), (m, with) => (auditableEntities.In(with, s => s.TableName.ToLower(), t => t.ToLower()), m))
                        .ForEach(q => q
                        .Iterate((t, m) =>
                        {
                        Log($"{m.Municipality}: {t.TableName} => Checking...");
                        var entityColumns = M5.GetColumnList(DBO_SCHEMA, m.ConnectionString, t.TableName);
                        var auditColumns = M5.GetColumnList(AUDIT_SCHEMA, m.ConnectionString, t.TableName);

                                        entityColumns.NotIn(auditColumns, s => s.Name.ToLower(), f => f.Name.ToLower())
                                            .DoIf(q2 => q2.Count() > 0, q3 => Log($"  {q3.Count()} missing fields"))
                                            .ForEach(f =>
                                            {
                                                Log($"  {m.Municipality}: {t.TableName} => Field {f} missing in audit table");
                        
                                                f
                                                    .GenerateMissingColumnSql(m, AUDIT\_SCHEMA, t.TableName)
                                                    .Execute(m.ConnectionString, (sql, e) => Log($"{m.Municipality}: {t.TableName} => {e.Message}\\r\\nFailed to execute SQL: {sql.ToString()}"));
                                            });
                                    }));
                            });
                        

                        This is a custom "one line" function that creates missing audit tables or adds the missing columns that the entity defines but someone forgot to add to the audit table. :-D

                        Latest Articles:
                        Client-Side Type-Based Publish

                        M Offline
                        M Offline
                        Member 11570523
                        wrote on last edited by
                        #29

                        Nice! However, I'm surprised you used old fashioned SQL to do the database update in light of what you are trying to illustrate.

                        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