如何将group_concat,join和union查询转换为linq lambda?

时间:2015-10-14 15:46:40

标签: c# mysql asp.net-mvc linq lambda

我对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))
                });  

我感觉我很亲近......

1 个答案:

答案 0 :(得分:1)

我认为这会让你得到你想要的东西。这是未经测试的,如果你有问题让我知道,我会解决。

var allitems =
    _programContentService
    .Products
    .Select(x => new { x.FullName, x.TypeId, x.Id })
    .Union(
        _programContentService
        .Programs
        .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,
        x => x.Category,
        specialty => specialty.Id,
        (x, specialty) => new { x.FullName, specialty.CategoryName })
    .GroupBy(x => x.FullName)
    .AsEnumerable()
    .Select(x => new SelectListItem 
        { 
            Value = x.Key, 
            Text = String.Join(",", x.Select(y => y.CategoryName))
        });