【问题标题】:MVC3 Linq query adding to listMVC3 Linq 查询添加到列表
【发布时间】:2012-12-17 22:41:23
【问题描述】:

我正在尝试使用查询从db.tblSoftwares 构建软件名称列表。

我尝试使用.Add 方法,但它不起作用。

任何建议将不胜感激。

public ActionResult Details(int id)
{
   // this gets the hostname based on id//
   tblComputer tblcomputer = db.tblComputers.Find(id);
   var workstation  = tblcomputer.HostName;

   // next want to get the list of software names assigned to host name//

   // get the list of licenses for hostname based on id/
   var Licenses = (from n in db.tblLicenses
                   where n.tblComputerComputerId == id
                   select n.tblSoftwareSoftwareId).ToList();

   List<string> appslist = new List<string>();

   foreach (var l in Licenses)
   {
       var query1 = (from s in db.tblSoftwares
                     where s.SoftwareId == l
                     select new { s.Name }).ToList();

       // appslist = query1;           
   }                         

   var swList = new List<string> { "Adobe", "MS Office", "MS Visio" };

   var ViewModel = new ComputerSoftwareViewModel
   {
       HostName = workstation,
       Applications = swList                
   };

   return View(ViewModel);
}

【问题讨论】:

  • 我在问问题之前发布了:我确信有更好的方法可以做到这一点,但这是我能理解的最好的方法。我正在尝试使用查询从 db.tblSoftwares 构建软件名称列表。我尝试使用 .add 方法,但它不起作用。任何建议,将不胜感激。谢谢..

标签: c# list generics


【解决方案1】:

加入两个表,直接选择新的列表。

试试这个: C# Joins/Where with Linq and Lambda

【讨论】:

    猜你喜欢
    • 2022-10-12
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多