简体   繁体   English

使用 2 个表实现 ASP.NET MVC5 搜索功能

[英]Implement ASP.NET MVC5 search functionality using 2 tables

I have created two tables: Claim and ClaimAttachments.我创建了两个表:Claim 和 ClaimAttachments。 I'm trying to join them on ClaimID in order to get the filtered data from both the tables.我正在尝试在 ClaimID 上加入它们,以便从两个表中获取过滤后的数据。

public ActionResult Index(int? search) {公共动作结果索引(int?搜索){

        if (search!=null)
        {
            var Product = (from P in db.Claims
                           join C in db.ClaimAttachments on
                           P.ClaimID equals C.ClaimID

                           select new Claim
                           {
                               ClaimID = P.ClaimID,
                               ClaimBatchID = P.ClaimBatchID,
                               PatientControlNumber = P.PatientControlNumber,
                               PatientFirstName = P.PatientFirstName,
                               PatientLastName = P.PatientLastName,
                               ServiceFromDate = P.ServiceFromDate,
                               ServiceToDate = P.ServiceToDate,
                           });
             return View(db.Claims.Where(x => x.ClaimID == search).ToList());
           

        }
        
        else
        {
            return View(db.Claims.ToList());
        }

I'm able to get the searched result but from single table.我可以从单个表中获得搜索结果。 The join is not working.联接不起作用。

Currently you're only selecting from the Claims data:目前,您仅从Claims数据中进行选择:

return View(db.Claims.Where(x => x.ClaimID == search).ToList());

You have a join query just above that line of code:您在该代码行上方有一个join查询:

var Product = (from P in db.Claims
               join C in db.ClaimAttachments on
               P.ClaimID equals C.ClaimID

               select new Claim
               {
                   ClaimID = P.ClaimID,
                   ClaimBatchID = P.ClaimBatchID,
                   PatientControlNumber = P.PatientControlNumber,
                   PatientFirstName = P.PatientFirstName,
                   PatientLastName = P.PatientLastName,
                   ServiceFromDate = P.ServiceFromDate,
                   ServiceToDate = P.ServiceToDate
               });

But you don't do anything with the results of that query.但是您不会对该查询的结果做任何事情。 It sounds like you meant to use the results of that query (which is in the Product variable, which incidentally should probably have a plural name since it's a collection) instead of just selecting from db.Claims .听起来您打算使用该查询的结果(在Product变量中,由于它是一个集合,它可能应该有一个复数名称),而不是仅仅从db.Claims中选择。 Something like this:像这样的东西:

return View(Product.Where(x => x.ClaimID == search).ToList());

Note however that you're still only selecting data from one table.但是请注意,您仍然只从一个表中选择数据 Though the join operation may alter the results of that selection.尽管join操作可能会改变该选择的结果。 But the selection itself is here:但选择本身就在这里:

select new Claim
{
    ClaimID = P.ClaimID,
    ClaimBatchID = P.ClaimBatchID,
    PatientControlNumber = P.PatientControlNumber,
    PatientFirstName = P.PatientFirstName,
    PatientLastName = P.PatientLastName,
    ServiceFromDate = P.ServiceFromDate,
    ServiceToDate = P.ServiceToDate
}

Notice how every value selected is from the P alias, which is defined here:请注意,选择的每个值都来自P别名,它在此处定义:

from P in db.Claims

So you're successfully joining the two tables, but only selecting data from one of the two tables.因此,您成功地连接了两个表,但只从两个表之一中选择数据。 If you want to also select data from the other table then, well, you need to select data from the other table.如果您还想从另一个表中获取 select 数据,那么,您需要从另一个表中获取 select 数据。 For example, if there's a property on that table called SomeProperty that you want to select then you'd need to select it, and into an object which has that property.例如,如果该表上有一个名为SomeProperty的属性,您想要 select 那么您需要 select 它,然后进入一个 object 它具有该属性。

For example, you might create a view model (let's call it ClaimViewModel as an example) which represents a combined record of the two tables, containing the properties you want from each.例如,您可以创建一个视图 model(我们称之为ClaimViewModel作为示例),它表示两个表的组合记录,其中包含您想要的每个表的属性。 Then you'd select into that type:然后您将 select 转换为该类型:

select new ClaimViewModel
{
    ClaimID = P.ClaimID,
    ClaimBatchID = P.ClaimBatchID,
    PatientControlNumber = P.PatientControlNumber,
    PatientFirstName = P.PatientFirstName,
    PatientLastName = P.PatientLastName,
    ServiceFromDate = P.ServiceFromDate,
    ServiceToDate = P.ServiceToDate,
    SomeProperty = C.SomeProperty // <--- here
}

This would select the combined data into a list of ClaimViewModel objects, which you'd then filter based on your "search" and return to your view just like you do with the Claims objects now.这会将 select组合数据放入ClaimViewModel对象列表中,然后您将根据您的“搜索”对其进行过滤并返回您的视图,就像您现在处理Claims对象一样。 And of course that view would need to be updated to expect a collection of ClaimViewModel objects instead of a collection of Claim objects.当然,该视图需要更新以期望获得ClaimViewModel对象的集合,而不是Claim对象的集合。

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

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