LINQ帮助新手

时间:2012-07-03 14:17:06

标签: .net sql asp.net-mvc-3 linq

无法获得此功能,有人可以为此LINQ查询提供帮助吗?

select col1, 
(select col2 from tbl2 where tbl2.ID=tbl1.tbl2ID) as [col2] 
from tbl1  

场景就像这样

我想要来自Jobs表的所有记录,Jobs都有sectorID列,我也想要SectorName。它还有CountryID和CityID,我也需要CountryName和CityName。

4 个答案:

答案 0 :(得分:1)

如果关系设置得当,我认为它应该是这样的:

var result=from job in Jobs
           select new {job.jobID, job.jobName, job.Sector.SectorName, job.Country.CountryName, job.City.CityName};

答案 1 :(得分:0)

你应该JOIN这些表格如下:

from j in Jobs
join c in Countries on j.CountryID equals c.ID
join c2 in Cities on c.CityID equals c2.ID
select new 
{ 
    SectorName = j.SectorName,
    CountryName = c.Name,
    CityName = c2.Name
};

答案 2 :(得分:0)

这是一个很好的样本,我发现我认为解释了你在寻找什么。绝对需要一个JOIN。

class Job
{
public int ID { get; set; }
public string Name { get; set; }
public string Title { get; set; }
}

class Sector
{
public int ID { get; set; }
public string SectorName { get; set; }
}

class City
{
public int ID { get; set; }
public string CityName { get; set; }
}

void Main()
{
var jobs = new Job[]
{
    new Job{ID = 5, Name = "Sam", Title="Minion"},
    new Job{ID = 6, Name = "Dave", Title="Overlord"},
    new Job{ID = 7, Name = "Julia", Title="Minion"},
    new Job{ID = 8, Name = "Sue", Title="Minion"}
};

// Example orders.
var sectors = new Sector[]
{
    new Sector{ID = 5, SectorName = "Sector42"}
};

var cities = new City[]
{
    new City{ID = 5, CityName = "TownsVille"}
};

// Join on the ID properties.
var query = from j in jobs
        join s in sectors on j.ID equals s.ID
        join c in cities on j.ID equals c.ID
        select new { j.Name, j.Title, c.CityName, s.SectorName };

// Display joined groups.
foreach (var group in query)
{
    Console.WriteLine("{0} Is a  {1} in {2}, sector {3}", group.Name, group.Title, group.CityName, group.SectorName);
}

}

输出看起来像这样(来自foreach循环)

Sam Is a  Minion in TownsVille, sector Sector42

我引用了这个网站:http://www.dotnetperls.com/join

答案 3 :(得分:0)

你需要做一些加入。不知道你的结构,类似下面的东西可能会起作用。

创建一个简单的视图模型来保存您的信息:

public class JobView
{
    public int JobId { get; set; }
    public string JobName { get; set; }
    //Add any other fields you may need here
    public string SectorName { get; set; }
    public string CityName { get; set; }
    public string CountryName { get; set; }
    public string CityName { get; set; }

}

然后根据您的需要填充列表

List<JobView> retVal = (from job in Jobs
                join sector in Sectors on job.SectorId equals sector.Id
                join country in Countries on job.CountryId equals country.Id
                join city in Cities on job.CityId equals city.Id
                select new JobView { 
                                JobId = job.Id,
                                JobName = job.JobName,
                                SectorName = sector.SectorName,
                                CountryName = country.CountryName,
                                CityName = city.CityName }).ToList();
    }

在选择期间,只需要填充JobView中定义的任何其他字段。