简体   繁体   English

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

[英]How to convert a group_concat, join, and union query into linq lambda?

im pretty new to linq-lambda. 我对linq-lambda很陌生。 I have a MySQL query that pulls the name of items in two tables union'd together. 我有一个MySQL查询,该查询将两个表中的项目名称合并在一起。 Then pull another column of the specialties that the items fall under. 然后拉出该项目所属专业的另一列。

my working query is this: 我的工作查询是这样的:

 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

now my problem is... that i have to somehow convert this into linq lambda. 现在我的问题是...我必须以某种方式将其转换为linq lambda。 My attempt is this: 我的尝试是这样的:

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??"  });

UPDATE : 更新

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

i have a feeling that I am close... 我有种亲近的感觉...

I think this will get you what you want. 我认为这将为您提供所需的东西。 It is untested, if you have issues let me know and I will work it out. 它未经测试,如果您有任何问题,请告诉我,我会解决。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM