asp.net 从不同的数据库连接获取数据到列表c#

v9tzhpje  于 2023-04-22  发布在  .NET
关注(0)|答案(1)|浏览(121)

在我的ASP.NETMVC应用程序中,我使用了两个数据上下文。
下面是我当前的应用程序,我想从database01中获取一些数据,并将这些数据与database02连接起来,创建一个列表,并返回一个视图。
这是我现在的代码。

private zSqlLink dbs = new zSqlLink();
private zSqlData db = new zSqlData();
// GET: Selfcare

public ActionResult PendingSelfTasks() 
{
  IEnumerable <SelfCareTasks> pendingTasks = new List <SelfCareTasks> ();

  var list = (from l in dbs.SelfCareTasks where l.Is_Service_Accepted == false 
              && l.Status == true select new {
              l.Id, 
              l.Service_Id, 
              l.Customer_Id, 
              l.Service_End_Date, 
              l.Service_Price
              }
              ).ToList();

  pendingTasks = (from d in list
                  join c in db.Customer on d.Customer_Id equals c.Id 
                  join ser in db.Services on d.Service_Id equals ser.Id 
                  select new SelfCareTasks {
                   Id = d.Id,
                   CustomerName = c.Sur_Name + " " + c.Name,
                   ServiceName = ser.Service_NameEng,
                   ServicePrice = d.Service_Price,
                   EndDate = d.Service_End_Date.ToString("dd-MMM-yyyy")

                 }).ToList();

  return View(pendingTasks);
}

第一个var list显示列表中有2个数据。
但是当它与PendingTasks列表连接时,结果变为null。
还有其他解决办法吗?

c0vxltue

c0vxltue1#

有三种方法:

Bring the results from both tables into memory and use LINQ to Objects to join them. (Only recommended for a small number of records because of memory/processor overhead)
Add one of the tables to the other data context (Copy the DBML over and prefix the name attribute with the name of the database, e.g. database2.dbo.MyTable) (it seem this is added later, because I remembered he suggested to create a view here)

另一种方法是

3. Create a View at the database that get the data from the two tables in the two database.

相关问题