我有2个字段的数据库表:指数(INT),电子邮件(VARCHAR(100))复杂LINQ查询
我需要做到以下几点:
- 集团所有电子邮件域名(所有电子邮件已经小写)。
- 选择从所有组中的所有电子邮件,其中电子邮件域不超过总邮件的20%步骤之前1.
代码示例的总和:
DataContext db = new DataContext();
//Domains to group by
List<string> domains = new List<string>() { "gmail.com", "yahoo.com", "hotmail.com" };
Dictionary<string, List<string>> emailGroups = new Dictionary<string, List<string>>();
//Init dictionary
foreach (string thisDomain in domains)
{
emailGroups.Add(thisDomain, new List<string>());
}
//Get distinct emails
var emails = db.Clients.Select(x => x.Email).Distinct();
//Total emails
int totalEmails = emails.Count();
//One percent of total emails
int onePercent = totalEmails/100;
//Run on each email
foreach (var thisEmail in emails)
{
//Run on each domain
foreach (string thisDomain in emailGroups.Keys)
{
//If email from this domain
if (thisEmail.Contains(thisDomain))
{
//Add to dictionary
emailGroups[thisDomain].Add(thisEmail);
}
}
}
//Will store the final result
List<string> finalEmails = new List<string>();
//Run on each domain
foreach (string thisDomain in emailGroups.Keys)
{
//Get percent of emails in group
int thisDomainPercents = emailGroups[thisDomain].Count/onePercent;
//More than 20%
if (thisDomainPercents > 20)
{
//Take only 20% and join to the final result
finalEmails = finalEmails.Union(emailGroups[thisDomain].Take(20 * onePercent)).ToList();
}
else
{
//Join all to the final result
finalEmails = finalEmails.Union(emailGroups[thisDomain]).ToList();
}
}
有谁知道一个更好的办法做了?
Linq to * what *? –
看起来像你只是想以某种方式过滤所有的结果,分组只是实现这一目标的一个跳板?顺便说一句,你能否更清楚地说明为什么'101,102'而不是'100,101',对于'104,105'而不是'103,104'是一样的?收集项目从下到上? –
如果总数超过总数或者您想要包含所有达到阈值的电子邮件,您是否可以确认是否要完全排除域? – James