【发布时间】:2015-10-14 15:46:39
【问题描述】:
我对 linq-lambda 很陌生。我有一个 MySQL 查询,它将两个表中的项目名称合并在一起。然后拉出另一列项目所属的专业。
我的工作查询是这样的:
Select
p.title,
GROUP_CONCAT(spec.categoryname) genre
From
(SELECT FullName AS title, TypeId as typeid, Id as id FROM programs
UNION
SELECT FullName, TypeId, Id FROM products)
AS p
Inner join specialtymembers mem on (ItemType = p.typeid AND ItemId = p.id)
Inner join specialties spec on mem.Category = spec.id
GROUP BY p.title
ORDER BY p.title
现在我的问题是......我必须以某种方式将其转换为 linq lambda。我的尝试是这样的:
var allitems =
_programContentService.Products.Select(r => r.FullName)
.Union(_programContentService.Programs.Select(q => q.FullName))
.Join(_programContentService.SpecialtyMembers, z => z.ItemType, q => q.ItemType,
(z, q) => new {z, q})
.Join(_programContentService.Specialties, x => x.Id, z => z.Category,
(x, z) => new {x,z})
.Select(@t => new SelectListItem { Name = q.FillName.ToString(), Genre = "SOME HOW HAVE TO USE A LOOPING FEATURE??" });
更新:
var allitems = _programContentService
.ProgramProductViews
.Select(x => new {x.FullName, x.TypeId, x.Id})
.Join(
_programContentService.SpecialtyMembers,
type => new {type.TypeId, type.Id},
member => new {TypeId = member.ItemType, Id = member.ItemId},
(type, member) => new {type.FullName, member.Category})
.Join(
_programContentService.Specialties,
q => q.Category,
specialty => specialty.Id,
(q, specialty) => new { q.FullName, specialty.SpecialtyName })
.GroupBy(x=>x.FullName)
.AsEnumerable()
.Select(x => new SelectListItem
{
Value = x.Key,
Text = String.Join(",", x.Select(q=>q.SpecialtyName))
});
我感觉我很亲近……
【问题讨论】:
标签: c# mysql asp.net-mvc linq lambda