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. General Programming
  3. C#
  4. async using linq

async using linq

Scheduled Pinned Locked Moved C#
linqcsharpfunctionalhelptutorial
7 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.
  • U Offline
    U Offline
    User 13446657
    wrote on last edited by
    #1

    Hi, I have he following code which for the line "await GetSkorekortSkore(x.SkorekortID)" is giving the error message "the await operator can only be used within an async lambda expression. Consider marking this lambda expression with the async modifier". Any suggestion of how to make it work ?

    return (from s in _db.SkoreKort
    from k in _db.Klub
    from b in _db.Bane
    where (s.SkorekortID == SkorekortID &&
    s.BaneKlubID == k.KlubID &&
    s.BaneID == b.BaneID)
    select new {
    s.SkorekortID,
    k.DGUKlubNr,
    s.SkoreDato,
    s.SpillerHCP,
    k.KlubNavn,
    b.BaneNavn,
    b.TeeNavn,
    b.HerreDame,
    b.BaneID,
    k.KlubID
    }).AsQueryable()
    .Select(x => new SkorekortDetailViewModel {
    SkorekortID = x.SkorekortID,
    DGUKlubNr = x.DGUKlubNr,
    SkoreDato = x.SkoreDato,
    SpillerHCP = x.SpillerHCP,
    KlubNavn = x.KlubNavn,
    BaneNavn = x.BaneNavn,
    TeeNavn = x.TeeNavn,
    HerreDame = x.HerreDame,
    SkoreListe = await GetSkorekortSkore(x.SkorekortID)
    }).ToListAsync();

    }
    
    public async Task<List<SkorekortSkore>> GetSkorekortSkore(int SkorekortID)
    {
    
    
      return await (from s in \_db.SkoreKortSkore
                    where s.SkorekortID == SkorekortID
                    select s).ToListAsync();
     
    }
    
    P L 2 Replies Last reply
    0
    • U User 13446657

      Hi, I have he following code which for the line "await GetSkorekortSkore(x.SkorekortID)" is giving the error message "the await operator can only be used within an async lambda expression. Consider marking this lambda expression with the async modifier". Any suggestion of how to make it work ?

      return (from s in _db.SkoreKort
      from k in _db.Klub
      from b in _db.Bane
      where (s.SkorekortID == SkorekortID &&
      s.BaneKlubID == k.KlubID &&
      s.BaneID == b.BaneID)
      select new {
      s.SkorekortID,
      k.DGUKlubNr,
      s.SkoreDato,
      s.SpillerHCP,
      k.KlubNavn,
      b.BaneNavn,
      b.TeeNavn,
      b.HerreDame,
      b.BaneID,
      k.KlubID
      }).AsQueryable()
      .Select(x => new SkorekortDetailViewModel {
      SkorekortID = x.SkorekortID,
      DGUKlubNr = x.DGUKlubNr,
      SkoreDato = x.SkoreDato,
      SpillerHCP = x.SpillerHCP,
      KlubNavn = x.KlubNavn,
      BaneNavn = x.BaneNavn,
      TeeNavn = x.TeeNavn,
      HerreDame = x.HerreDame,
      SkoreListe = await GetSkorekortSkore(x.SkorekortID)
      }).ToListAsync();

      }
      
      public async Task<List<SkorekortSkore>> GetSkorekortSkore(int SkorekortID)
      {
      
      
        return await (from s in \_db.SkoreKortSkore
                      where s.SkorekortID == SkorekortID
                      select s).ToListAsync();
       
      }
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You haven't included the method definition for the top LINQ statement but that message is telling you that that method also needs to be async.

      This space for rent

      U 1 Reply Last reply
      0
      • P Pete OHanlon

        You haven't included the method definition for the top LINQ statement but that message is telling you that that method also needs to be async.

        This space for rent

        U Offline
        U Offline
        User 13446657
        wrote on last edited by
        #3

        This is how it looks:

        namespace Golf.Components
        {
        public class SkorekortDetailViewComponent : ViewComponent
        {
        public readonly GolfContext _db;

        public SkorekortDetailViewComponent(GolfContext context)
        {
          \_db = context;
        }
        
        
        public async Task InvokeAsync(int SkorekortID)
        {
          var items = await GetItemsAsync(SkorekortID);
          return View(items);
        }
        
        private async Task\> GetItemsAsync(int SkorekortID)
        {
          
          return (from s in \_db.SkoreKort
                  from k in \_db.Klub
                  from b in \_db.Bane
                  where (s.SkorekortID == SkorekortID &&
                         s.BaneKlubID == k.KlubID &&
                         s.BaneID == b.BaneID)
                  select new {
                    s.SkorekortID,
                    k.DGUKlubNr,
                    s.SkoreDato,
                    s.SpillerHCP,
                    k.KlubNavn,
                    b.BaneNavn,
                    b.TeeNavn,
                    b.HerreDame,
                    b.BaneID,
                    k.KlubID          
                  }).AsQueryable() 
                                   .Select(x => new SkorekortDetailViewModel {
                                     SkorekortID = x.SkorekortID,
                                     DGUKlubNr = x.DGUKlubNr,
                                     SkoreDato = x.SkoreDato,
                                     SpillerHCP = x.SpillerHCP,
                                     KlubNavn = x.KlubNavn,
                                     BaneNavn = x.BaneNavn,
                                     TeeNavn = x.TeeNavn,
                                     HerreDame = x.HerreDame,                                 
                                     SkoreListe = await GetSkorekortSkore(x.SkorekortID)
                                   }).ToListAsync();
          
        }
        
        public async Task\> GetSkorekortSkore(int SkorekortID)
        {
        
        
          return await (from s in \_db.SkoreKortSkore
                        where s.SkorekortID == SkorekortID
                        select s).ToListAsync();
         
        }
        
        P 1 Reply Last reply
        0
        • U User 13446657

          This is how it looks:

          namespace Golf.Components
          {
          public class SkorekortDetailViewComponent : ViewComponent
          {
          public readonly GolfContext _db;

          public SkorekortDetailViewComponent(GolfContext context)
          {
            \_db = context;
          }
          
          
          public async Task InvokeAsync(int SkorekortID)
          {
            var items = await GetItemsAsync(SkorekortID);
            return View(items);
          }
          
          private async Task\> GetItemsAsync(int SkorekortID)
          {
            
            return (from s in \_db.SkoreKort
                    from k in \_db.Klub
                    from b in \_db.Bane
                    where (s.SkorekortID == SkorekortID &&
                           s.BaneKlubID == k.KlubID &&
                           s.BaneID == b.BaneID)
                    select new {
                      s.SkorekortID,
                      k.DGUKlubNr,
                      s.SkoreDato,
                      s.SpillerHCP,
                      k.KlubNavn,
                      b.BaneNavn,
                      b.TeeNavn,
                      b.HerreDame,
                      b.BaneID,
                      k.KlubID          
                    }).AsQueryable() 
                                     .Select(x => new SkorekortDetailViewModel {
                                       SkorekortID = x.SkorekortID,
                                       DGUKlubNr = x.DGUKlubNr,
                                       SkoreDato = x.SkoreDato,
                                       SpillerHCP = x.SpillerHCP,
                                       KlubNavn = x.KlubNavn,
                                       BaneNavn = x.BaneNavn,
                                       TeeNavn = x.TeeNavn,
                                       HerreDame = x.HerreDame,                                 
                                       SkoreListe = await GetSkorekortSkore(x.SkorekortID)
                                     }).ToListAsync();
            
          }
          
          public async Task\> GetSkorekortSkore(int SkorekortID)
          {
          
          
            return await (from s in \_db.SkoreKortSkore
                          where s.SkorekortID == SkorekortID
                          select s).ToListAsync();
           
          }
          
          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Try

          return await (from s in _db.SkoreKort

          You're attempting to return an async operation without actually specifying that it's async.

          This space for rent

          U 1 Reply Last reply
          0
          • P Pete OHanlon

            Try

            return await (from s in _db.SkoreKort

            You're attempting to return an async operation without actually specifying that it's async.

            This space for rent

            U Offline
            U Offline
            User 13446657
            wrote on last edited by
            #5

            It didn't change the error, it is still the same. The error message suggest to add async in front of x => new SkorekortDetailViewModel as

            }).AsQueryable()
            .Select(async x => new SkorekortDetailViewModel {
            SkorekortID = x.SkorekortID,
            DGUKlubNr = x.DGUKlubNr,
            SkoreDato = x.SkoreDato,
            SpillerHCP = x.SpillerHCP,
            KlubNavn = x.KlubNavn,
            BaneNavn = x.BaneNavn,
            TeeNavn = x.TeeNavn,
            HerreDame = x.HerreDame,
            SkoreListe = await GetSkorekortSkore(x.SkorekortID),
            HulListe = await GetHuller(x.BaneID),
            BaneListe = await GetBane(x.KlubID)
            }).ToListAsync();

            but that just shows all the linq as in error.

            1 Reply Last reply
            0
            • U User 13446657

              Hi, I have he following code which for the line "await GetSkorekortSkore(x.SkorekortID)" is giving the error message "the await operator can only be used within an async lambda expression. Consider marking this lambda expression with the async modifier". Any suggestion of how to make it work ?

              return (from s in _db.SkoreKort
              from k in _db.Klub
              from b in _db.Bane
              where (s.SkorekortID == SkorekortID &&
              s.BaneKlubID == k.KlubID &&
              s.BaneID == b.BaneID)
              select new {
              s.SkorekortID,
              k.DGUKlubNr,
              s.SkoreDato,
              s.SpillerHCP,
              k.KlubNavn,
              b.BaneNavn,
              b.TeeNavn,
              b.HerreDame,
              b.BaneID,
              k.KlubID
              }).AsQueryable()
              .Select(x => new SkorekortDetailViewModel {
              SkorekortID = x.SkorekortID,
              DGUKlubNr = x.DGUKlubNr,
              SkoreDato = x.SkoreDato,
              SpillerHCP = x.SpillerHCP,
              KlubNavn = x.KlubNavn,
              BaneNavn = x.BaneNavn,
              TeeNavn = x.TeeNavn,
              HerreDame = x.HerreDame,
              SkoreListe = await GetSkorekortSkore(x.SkorekortID)
              }).ToListAsync();

              }
              
              public async Task<List<SkorekortSkore>> GetSkorekortSkore(int SkorekortID)
              {
              
              
                return await (from s in \_db.SkoreKortSkore
                              where s.SkorekortID == SkorekortID
                              select s).ToListAsync();
               
              }
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Were any parts actually working? I would classify this as "unmaintainable"; and getting it to actually run would be bad karma. You need to start with (working) "sub queries" that can be combined to arrive at the desired result; and where intermediate results can be checked / dumped if needed. (Even the "SQL query plan optimizer" needs help sometimes).

              "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

              U 1 Reply Last reply
              0
              • L Lost User

                Were any parts actually working? I would classify this as "unmaintainable"; and getting it to actually run would be bad karma. You need to start with (working) "sub queries" that can be combined to arrive at the desired result; and where intermediate results can be checked / dumped if needed. (Even the "SQL query plan optimizer" needs help sometimes).

                "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                U Offline
                U Offline
                User 13446657
                wrote on last edited by
                #7

                It works fine without the line "SkoreListe = await GetSkorekortSkore(x.SkorekortID)".

                I'm not sure if it can work the way i thought it should work, so I have changed it to this instead which works:

                private async Task\> GetItemsAsync(int SkorekortID)
                {
                  
                  List skorekortdetail = await (from s in \_db.SkoreKort
                          from k in \_db.Klub
                          from b in \_db.Bane
                          where (s.SkorekortID == SkorekortID &&
                                 s.BaneKlubID == k.KlubID &&
                                 s.BaneID == b.BaneID)
                          select new {
                            s.SkorekortID,
                            k.DGUKlubNr,
                            s.SkoreDato,
                            s.SpillerHCP,                
                            k.KlubNavn,                
                            b.BaneNavn,
                            b.TeeNavn,
                            b.HerreDame,
                            b.BaneID,
                            k.KlubID
                          }).AsQueryable() 
                            .Select(x => new SkorekortDetailViewModel {
                              SkorekortID = x.SkorekortID,
                              DGUKlubNr = x.DGUKlubNr,
                              SkoreDato = x.SkoreDato,
                              SpillerHCP = x.SpillerHCP,
                              KlubNavn = x.KlubNavn,
                              BaneNavn = x.BaneNavn,
                              TeeNavn = x.TeeNavn,
                              HerreDame = x.HerreDame,
                              BaneID = x.BaneID,
                              KlubID = x.KlubID                  
                            }).ToListAsync();
                
                  foreach(var skorekort in skorekortdetail) {
                
                    skorekort.SkoreListe = await GetSkorekortSkoreAsync(skorekort.SkorekortID);
                    skorekort.HulListe = await GetHullerAsync(skorekort.BaneID);
                    skorekort.BaneListe = await GetBaneAsync(skorekort.KlubID);
                
                  }
                
                  return skorekortdetail;
                  
                }
                
                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