How to combine two query results together in C# & ASP.NET MVC controller

I have a table called Complaint_Log and the relevant column names in it are CompanyName and ContactPerson.

What I’m trying to do is generate a partial view for my dash page of a table that loops through the Complaint_Log data and if there is duplicate values of the company name or the contact person, then have it get generated to a list. I also need it to get a count of how many duplicates there are and get rid of the duplicates.

I currently have a query I’m using that works perfect, but it’s only for the CompanyName. I can’t figure out how to get it to loop through the list and include the ContactPerson (as the CompanyName) IF there is a duplicate of a ContactPerson, but doesn’t have the same CompanyName.

For example, lets say this is my list data below that’s in my table:

CompanyName ContactPerson
Company A Bob Smith
Company B Fred Stevens
Company A Rick Moore
Company C Bob Smith

So as you can see, there are 2 Company A‘s with a different ContactPerson and 2 Bob Smith’s with a different CompanyName.

In this example I’d like my table generated to show:

Customer Occurrences
Company A 2
Bob Smith 2

If both of the Company A entries had Bob Smith as the ContactPerson, then the above table should only show Company A. I want it set up this way in case there’s a person that maybe switches companies or something.

I’m totally stuck on how to achieve this, I literally just cannot figure this out. Maybe the way I’m currently going about it is not the way I should be, but as of right now, I created a model called CompanyAlertModel, a model called ContactAlertModel and a model called CustomerAlertModel. They are all identical except for the name of the models. I’m sure I probably don’t need that many models, but since my query worked for the CompanyName like I wanted it to, I made another one doing the same query except I did it for the ContactName.

My goal was to create that third model and combine the queries for the CompanyName and for the ContactName and add it into the third model which is what I’d use for my table I’d generate, but I can’t seem to figure out how to combine them.

This is my model (like I said the other ones are the same except for the names):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class CompanyAlertModel
{
public CompanyAlertModel() { }
public string Customer { get; set; }
public Nullable<int> Occurrence { get; set; }
public Complaint_Log Complaints { get; set; }
}
</code>
<code>public class CompanyAlertModel { public CompanyAlertModel() { } public string Customer { get; set; } public Nullable<int> Occurrence { get; set; } public Complaint_Log Complaints { get; set; } } </code>
public class CompanyAlertModel
{
    public CompanyAlertModel() { }

    public string Customer { get; set; }
    public Nullable<int> Occurrence { get; set; }
    public Complaint_Log Complaints { get; set; }
}

Here is my controller:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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());
}
</code>
<code>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()); } </code>
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());
}

And my table on the view page:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <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>
</code>
<code> <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> </code>
 <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>

Any help would be greatly appreciated!

6

In your case, you can generate a generic model to hold the values from your grouped lists. First create a generic view model that we shall pass to your View:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class GenericModel
{
public GenericModel() { }
public string Customer { get; set; }
public Nullable<int> Occurrence { get; set; }
}
</code>
<code>public class GenericModel { public GenericModel() { } public string Customer { get; set; } public Nullable<int> Occurrence { get; set; } } </code>
public class GenericModel
{
    public GenericModel() { }

    public string Customer { get; set; }
    public Nullable<int> Occurrence { get; set; }
}

Now using the exact same piece of code logic that you have, you do a Union on the two grouped lists when using this GenericModel:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public ActionResult CustomerAlerts()
{
List<GenericModel> myModel=new List<GenericModel>();
// 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 GenericModel()
{
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 GenericModel()
{
Customer = g.Key,
Occurrence = g.Count(),
};
var resultForCompany = company.Where(x => x.Occurrence > 1).ToList();
var resultForContact= contact.Where(x => x.Occurrence > 1).ToList();
//Now merge the two lists since they have are of the same type:
myModel=resultForCompany.Union(resultForContact).ToList();
//Here you are sending to your partial view a model of type List<GenericModel>
return PartialView(myModel);
}
</code>
<code>public ActionResult CustomerAlerts() { List<GenericModel> myModel=new List<GenericModel>(); // 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 GenericModel() { 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 GenericModel() { Customer = g.Key, Occurrence = g.Count(), }; var resultForCompany = company.Where(x => x.Occurrence > 1).ToList(); var resultForContact= contact.Where(x => x.Occurrence > 1).ToList(); //Now merge the two lists since they have are of the same type: myModel=resultForCompany.Union(resultForContact).ToList(); //Here you are sending to your partial view a model of type List<GenericModel> return PartialView(myModel); } </code>
public ActionResult CustomerAlerts()
{
    List<GenericModel> myModel=new List<GenericModel>();
    // 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 GenericModel()
                      {
                          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 GenericModel()
                      {
                          Customer = g.Key,
                          Occurrence = g.Count(),
                      };

    var resultForCompany = company.Where(x => x.Occurrence > 1).ToList();
    var resultForContact= contact.Where(x => x.Occurrence > 1).ToList();
    
    //Now merge the two lists since they have are of the same type:
    myModel=resultForCompany.Union(resultForContact).ToList();
    
    //Here you are sending to your partial view a model of type List<GenericModel>
    return PartialView(myModel);
}

Now since we have generic model, your View will expect a Model of type List<GenericModel>. The rest of the display to show the data will be the same that you have posted in your question.

3

You have to combined results for two queries into the third response.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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
};
</code>
<code> 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 }; </code>
   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
                          };

If you need two results like models use for the View

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> var model1 = new companesResultModel();
var model2 = new contactsResultModel();
return PartialView((model1, model2));
</code>
<code> var model1 = new companesResultModel(); var model2 = new contactsResultModel(); return PartialView((model1, model2)); </code>
   var model1 = new companesResultModel();
    var model2 = new contactsResultModel();
    return PartialView((model1, model2));

In the View

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@model (companesResultModel model1, contactsResultModel model2)
@foreach (var item in model1)
@foreach (var item in model2)
</code>
<code>@model (companesResultModel model1, contactsResultModel model2) @foreach (var item in model1) @foreach (var item in model2) </code>
@model (companesResultModel model1, contactsResultModel model2)

@foreach (var item in model1)
@foreach (var item in model2)

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật