我有一个名为
Complaint_Log
的表,其中相关的列名称是 CompanyName
和 ContactPerson
。
我想做的是为循环遍历
Complaint_Log
数据的表的仪表板页面生成部分视图,如果公司名称或联系人的值重复,则将其生成为列表。我还需要它来计算有多少重复项并删除重复项。
我目前正在使用一个查询,效果非常好,但它仅适用于
CompanyName
。我不知道如何让它循环遍历列表并包含 ContactPerson
(作为 CompanyName
)如果存在 ContactPerson
的重复项,但没有相同的 CompanyName
。
例如,假设这是我表中下面的列表数据:
公司名称 | 联系人 |
---|---|
A公司 | 鲍勃·史密斯 |
B公司 | 弗雷德·史蒂文斯 |
A公司 | 里克摩尔 |
C公司 | 鲍勃·史密斯 |
如您所见,有 2 个
Company A
具有不同的 ContactPerson
,2 个 Bob Smith 具有不同的 CompanyName
。
在此示例中,我希望生成的表格显示:
客户 | 事件发生 |
---|---|
A公司 | 2 |
鲍勃·史密斯 | 2 |
如果 A 公司的两个条目都将 Bob Smith 作为
ContactPerson
,那么上表应该只显示 A 公司。我希望以这种方式设置,以防有人可能更换公司或其他什么。
我完全不知道如何实现这一目标,我真的无法弄清楚这一点。也许我目前的处理方式不是我应该的方式,但截至目前,我创建了一个名为
CompanyAlertModel
的模型,一个名为 ContactAlertModel
的模型和一个名为 CustomerAlertModel
的模型。除了型号名称外,它们都是相同的。我确信我可能不需要那么多模型,但由于我的查询对 CompanyName
有效,就像我想要的那样,我做了另一个查询,除了我对 ContactName
执行了相同的查询之外。
我的目标是创建第三个模型,并将
CompanyName
和 ContactName
的查询组合起来,并将其添加到第三个模型中,这是我将用于生成的表的模型,但我可以'似乎不知道如何将它们结合起来。
这是我的模型(就像我说的,除了名字之外其他都是一样的):
public class CompanyAlertModel
{
public CompanyAlertModel() { }
public string Customer { get; set; }
public Nullable<int> Occurrence { get; set; }
public Complaint_Log Complaints { get; set; }
}
这是我的控制器:
public ActionResult CustomerAlerts()
{
// Get list of all the companies with more than 1 occurrence
var company = from c in db.Complaint_Log
group c by c.CompanyName into g
orderby g.Key
select new CompanyAlertModel()
{
Customer = g.Key,
Occurrence = g.Count(),
};
// Get list of all the contact names with more than 1 occurrence
var contact = from c in db.Complaint_Log
group c by c.ContactPerson into g
orderby g.Key
select new ContactAlertModel()
{
Customer = g.Key,
Occurrence = g.Count(),
};
var result = company.ToList();
return PartialView(result.Where(x => x.Occurrence > 1).ToList());
}
我在视图页面上的表格:
<table>
<tr>
<th>
Customer Name
</th>
<th>
Occurrences
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@item.Customer
</td>
<td>
@item.Occurrence
</td>
</tr>
}
</table>
任何帮助将不胜感激!
您必须将两个查询的结果合并到第三个响应中。
var thirdResponse = from comp1 in company
join cont1 in contact
on comp1.Customer equals cont1.Customer
select new
{
CompanyName = comp1.Customer,
ContactPerson = cont1.Customer,
CompanyOccurrences = comp1.Occurrence,
ContactOccurrences = cont1.Occurrence
};
如果您需要两个结果,例如用于视图的模型
var model1 = new companesResultModel();
var model2 = new contactsResultModel();
return PartialView((model1, model2));
在视图中
@model (companesResultModel model1, contactsResultModel model2)
@foreach (var item in model1)
@foreach (var item in model2)