简体   繁体   English

(代码完成)组合两个任务列表(表格)以显示总结果

[英](Code-Completion) Combine two task lists (tables) in order to show the total results

Today I'm trying to achieve something that I'm having difficulties with the code-completion.今天我正在努力实现一些我在代码完成方面遇到困难的事情。 I'm trying to combine two tasks (which are from different types).我正在尝试结合两个任务(来自不同类型)。

Before reaching here, I've tried several scenarios.... Using WhenAll to combine Tasks (which I cannot do this since both tasks are different).在到达这里之前,我已经尝试了几种方案.... 使用 WhenAll 来组合任务(我不能这样做,因为两个任务不同)。

The last part of the code (last 3 lines) are not correct and I'll ask if someone can take a look on this:) (Also if the method arguments are okay for this matter).代码的最后一部分(最后 3 行)不正确,我会问是否有人可以看看这个:) (另外,如果方法 arguments 可以解决这个问题)。 All I want is to return the list of the total rows combined by the two tasks.我想要的只是返回两个任务组合的总行列表。

Thanks in advance!提前致谢! (Code is below) (代码如下)

public async Task<(ApplyPerfectMatches, ApplyNonPerfectMatches)>
                GetMatchesAsync(string currentUser)
            {
                // Perfect Matches
                var listPerfectMatches = _context.ApplyPerfectMatches
                    .Where(x => x.IdEmailInvestor == currentUser)
                     // Use AsNoTracking to disable EF change tracking
                     // Use ToListAsync to avoid blocking a thread
                     .AsNoTracking().ToListAsync();
    
                // Determine Total matches vs. Limit matches per user
                int totalMatches = _context.ApplyPerfectMatches.Where(x => x.IdEmailInvestor == currentUser).Count();
                int totalMatchesBy10 = totalMatches / 10;
    
                // n(n-1)/2 + 2 (n = 1...n+1 => 2, 3, 5, 8, 12, 17, 23, 30...)
                int limitMatches = totalMatches < 10 ? 3 : (totalMatchesBy10 * (totalMatchesBy10 - 1) / 2) + 2;
    
                var submitDate = _context.ApplyPerfectMatches
                    .Where(x => x.IdEmailInvestor == currentUser)
                    .OrderByDescending(x => x.DtSubmitInvestor)
                    .Select(x => x.DtSubmitInvestor)
                    .FirstOrDefault();
    
                int days = (DateTime.Now - submitDate).Days;
    
                if (days > 3)
                {
                    listPerfectMatches = _context.ApplyPerfectMatches
                    .Where(x => x.IdEmailInvestor == currentUser)
                    .Take(limitMatches)
                     // Use AsNoTracking to disable EF change tracking
                     // Use ToListAsync to avoid blocking a thread
                     .AsNoTracking().ToListAsync();
                }
                else
                {
                    listPerfectMatches = _context.ApplyPerfectMatches
                    .Where(x => x.IdEmailInvestor == currentUser)
                    .Take(3)
                     // Use AsNoTracking to disable EF change tracking
                     // Use ToListAsync to avoid blocking a thread
                     .AsNoTracking().ToListAsync();
                }            
    
                // Total Matches (Perfect + Non-Perfect Matches)
                var listNonPerfectMatches = _context.ApplyNonPerfectMatches
                    .Where(x => x.IdEmailInvestor == currentUser)
                     // Use AsNoTracking to disable EF change tracking
                     // Use ToListAsync to avoid blocking a thread
                     .AsNoTracking().ToListAsync();
    
    
                var finalResult = listPerfectMatches.Result.Concat(listNonPerfectMatches.Result).ToList();           
    
                //await Task.WhenAll(tasks);
                //return tasks.SelectMany(t => t.Result).ToList();
            }

Update (I don't mind the negative votes but if someone needs help, that same person might be confused with the problem...):更新(我不介意反对票,但如果有人需要帮助,同一个人可能会对问题感到困惑......):

result = await _context.ApplyTotalMatches
                // Only get entries for the current logged in user
                .Where(x => x.IdEmailInvestor == currentUser && x.UnionSetOrder == 1)
                .Take(limitMatches)
                .Concat(_context.ApplyTotalMatches
                .Where(x => x.IdEmailInvestor == currentUser && x.UnionSetOrder == 2))
                // Use AsNoTracking to disable EF change tracking
                // Use ToListAsync to avoid blocking a thread
                .AsNoTracking().ToListAsync();

You have to await a Task object.您必须await Task object。 This "unwraps" the result:这“展开”了结果:

public async (List<ApplyPerfectMatches> PerfectMatches, List<ApplyNonPerfectMatches> NonApplyPerfectMatches) GetMatchesAsync(string currentUser)
{
  List<ApplyPerfectMatches> listPerfectMatches = await _context.ApplyPerfectMatches
    .Where(x => x.IdEmailInvestor == currentUser)
    .Take(3)
    .AsNoTracking()
    .ToListAsync();

  List<ApplyNonPerfectMatches> listNonPerfectMatches = await _context.ApplyNonPerfectMatches
    .Where(x => x.IdEmailInvestor == currentUser)
    .AsNoTracking()
    .ToListAsync();

  return (listPerfectMatches, listNonPerfectMatches);   
}

async Task Run()
{
  (List<ApplyPerfectMatches> PerfectMatches, List<ApplyNonPerfectMatches> NonApplyPerfectMatches) result = await GetMatchesAsync("Admin");
}

Directly accessing Task.Result can cause a deadlock.直接访问Task.Result会导致死锁。 QueryableExtensions.ToListAsync is an asynchronous method. QueryableExtensions.ToListAsync是一种异步方法。 Asynchronous methods always return a Task<T> or Task and must be awaited using await in order to execute asynchronously.异步方法总是返回一个Task<T>Task并且必须使用await才能异步执行。

First, you need to define a new class:首先,您需要定义一个新的 class:

public class GetMatchesResult {
   public string IdEmailInvestor { set; get; }
   public bool IsPerfectMatch { set; get; }
   ... rest of properties/columns go here
}

Since your method is defined as由于您的方法定义为

async Task<(ApplyPerfectMatches, ApplyNonPerfectMatches)>

you need to change it to:您需要将其更改为:

async Task<List<GetMatchesResult>> GetMatches

Now, you need to update the queries to:现在,您需要将查询更新为:

 // Perfect Matches
 var listPerfectMatches = await _context.ApplyPerfectMatches
     .Where(x => x.IdEmailInvestor == currentUser)
     .Select(x => new GetMatchesResult {
         IdEmailInvestor = x.IdEmailInvestor,
         IsPerfectMatch = true,
         ... rest of properties go here
     })
     // Use ToListAsync to avoid blocking a thread
     .ToListAsync();
                     

You need to do the same with the listNonPerfectMatches, but set the IsPerfectMatch to false.您需要对 listNonPerfectMatches 执行相同的操作,但将 IsPerfectMatch 设置为 false。

In the end, you can do:最后,您可以执行以下操作:

return listPerfectMatches.Concat(listNonPerfectMatches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM