1

无法得到这个,有人可以帮助这个 LINQ 查询吗?

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

场景是这样的

我想要 Jobs 表中的所有记录,Jobs 有扇区 ID 列,我也想要 SectorName。它也有 CountryID 和 CityID,我也需要 CountryName 和 CityName。

4

4 回答 4

1

如果关系设置正确,我认为应该是这样的:

var result=from job in Jobs
           select new {job.jobID, job.jobName, job.Sector.SectorName, job.Country.CountryName, job.City.CityName};
于 2012-07-03T14:24:17.053 回答
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
};
于 2012-07-03T14:19:48.337 回答
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

于 2012-07-03T14:25:28.317 回答
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 中定义的任何其他字段都只需要在选择期间填充。

于 2012-07-03T14:28:56.927 回答