簡體   English   中英

如何將group_concat,join和union查詢轉換為linq lambda?

[英]How to convert a group_concat, join, and union query into 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))
                });  

我有種親近的感覺...

我認為這將為您提供所需的東西。 它未經測試,如果您有任何問題,請告訴我,我會解決。

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