简体   繁体   English

EF Core 3.1:如何将这三个查询合并为一个?

[英]EF Core 3.1 :How to merge these three queries in one?

This is my Modal class这是我的模态 class

public class Tag
    {
        public int Id { get; set; }
        public int UnitId { get; set; }
        public int MeasureId { get; set; }
        public int TagTypeId { get; set; }
}

i want fetch data from table and perform GroupBy on TagTypeId .the result i have mind as this json data:我想从表中获取数据并在TagTypeId上执行GroupBy 。我想到的结果是这个 json 数据:

Result:结果:

[
    {
        "title": "Actual",
        "key": 1,
        "disabled": false,
        "child": [
            {
                "title": "valve 0036",
                "key": "Tag1",
                "disabled": true,
                "measure": "Celsius",
                "tagType": "Actual",
                "child": null
            },
            {
                "title": "valve 0036",
                "key": "Tag2",
                "disabled": true,
                "measure": "Celsius",
                "tagType": "Actual",
                "child": null
            },
            {
                "title": "valve 0036",
                "key": "Tag21",
                "disabled": true,
                "measure": "Celsius",
                "tagType": "Actual",
                "child": null
            }
           
        ]
    },
    {
        "title": "Virtual",
        "key": 2,
        "disabled": false,
        "child": [
            {
                "title": "valve 0036",
                "key": "Tag36",
                "disabled": true,
                "measure": "Celsius",
                "tagType": "Virtual",
                "child": null
            },
            {
                "title": "valve 0036",
                "key": "Tag37",
                "disabled": true,
                "measure": "Celsius",
                "tagType": "Virtual",
                "child": null
            }
        ]
    },
    {
        "title": "Lab",
        "key": 3,
        "disabled": false,
        "child": [
            {
                "title": "valve 0036",
                "key": "Tag38",
                "disabled": true,
                "measure": "Celsius",
                "tagType": "Lab",
                "child": null
            }
        ]
    }
]

i write this code works but i was forced use there query for do.我编写此代码有效,但我被迫使用那里查询做。

 var tagsDataActual = await _context
                .Tags.Where(x => x.TagTypeId == 1)
                .GroupBy(x => new { x.TagTypeId, x.TagType.Title })
                .Select(x => new TagDto
                {
                    Title = x.Key.Title,
                    Key = x.Key.TagTypeId.ToString(),
                    Child = _context.Tags.Where(x => x.TagTypeId == 1)
                     .Select(x => new TagDto
                     {
                         Title = x.Title,
                         Measure = x.Measure.Title,
                         Key = "Tag" + x.Id.ToString(),
                         TagType = x.TagType.Title,
                         Disabled = true,
                         Child = null
                     })
                }).ToListAsync();

            var tagsDataVirtual = await _context
               .Tags
               .Where(x => x.TagTypeId == 2)
               .GroupBy(x => new { x.TagTypeId, x.TagType.Title })
               .Select(xs => new TagDto
               {
                   Title = xs.Key.Title,
                   Child = _context.Tags.Where(x => x.TagTypeId == 2)
                    .Select(x => new TagDto
                    {
                        Title = x.Title,
                        Measure = x.Measure.Title,
                        Key = "Tag" + x.Id.ToString(),
                        TagType = x.TagType.Title,
                        Disabled = true,
                        Child = null
                    })
               }).ToListAsync();

            var tagsDataLab = await _context
           .Tags
           .Where(x => x.TagTypeId == 3)
           .GroupBy(x => new { x.TagTypeId, x.TagType.Title })
           .Select(xs => new TagDto
           {
               Title = xs.Key.Title,
               Child = _context.Tags.Where(x => x.TagTypeId == 3)
                .Select(x => new TagDto
                {
                    Title = x.Title,
                    Measure = x.Measure.Title,
                    Key = "Tag" + x.Id.ToString(),
                    TagType = x.TagType.Title,
                    Disabled = true,
                    Child = null
                })
           }).ToListAsync();
            var tagsData = tagsDataActual.Concat(tagsDataVirtual).Concat(tagsDataLab).ToList();
            return tagsData;

and this is TagDto Class这是 TagDto Class

public class TagDto
{

    public string Title { get; set; }
    public string Key { get; set; }
    public bool Disabled { get; set; }
    public string Measure { get; set; }
    public string TagType { get; set; }
    public IEnumerable<TagDto> Child { get; set; }
}

is there any way for use one query instead of there?有没有办法使用一个查询而不是那里? thanks...谢谢...

Update your Where(...) to be like .Where(x => x.TagTypeId == 1 || x.TagTypeId == 2 || x.TagTypeId == 3) .将您的Where(...)更新为.Where(x => x.TagTypeId == 1 || x.TagTypeId == 2 || x.TagTypeId == 3)

Update your Child = line as mentioned below.如下所述更新您的Child =行。 Here x is already Grouped list for respective TagTypeId , so you don't need to filter again.这里x已经是各个TagTypeIdGrouped list ,因此您无需再次过滤。 Just use Child = x.Select(y => new TagDto {...} . Don't use x again as it is used in outer scope .只需使用Child = x.Select(y => new TagDto {...} 。不要再次使用x ,因为它在outer scope中使用。

Write your query like below.像下面这样写你的查询。

var tagsData = await _context.Tags
                .Where(x => x.TagTypeId == 1 || x.TagTypeId == 2 || x.TagTypeId == 3)
                .GroupBy(x => new { x.TagTypeId, x.TagType.Title })
                .Select(x => new TagDto
                {
                    Title = x.Key.Title,
                    Key = x.Key.TagTypeId.ToString(),
                    Child = x.Select(y => new TagDto
                             {
                                 Title = y.Title,
                                 Measure = y.Measure.Title,
                                 Key = "Tag" + y.Id.ToString(),
                                 TagType = y.TagType.Title,
                                 Disabled = true,
                                 Child = null
                             })
                }).ToListAsync();

Edit Try like below query.编辑尝试如下查询。

Update your Where(...) to be like .Where(x => x.TagTypeId == 1 || x.TagTypeId == 2 || x.TagTypeId == 3) .将您的Where(...)更新为.Where(x => x.TagTypeId == 1 || x.TagTypeId == 2 || x.TagTypeId == 3)

Update your Where part in Child = _context.Tags.Where(y => y.TagTypeId == x.Key.TagTypeId) .更新Child = _context.Tags.Where(y => y.TagTypeId == x.Key.TagTypeId)中的Where部分。

Complete query will be like below.完整的查询将如下所示。

var tagsData = await _context.Tags
                .Where(x => x.TagTypeId == 1 || x.TagTypeId == 2 || x.TagTypeId == 3)
                .GroupBy(x => new { x.TagTypeId, x.TagType.Title })
                .Select(x => new TagDto
                {
                    Title = x.Key.Title,
                    Key = x.Key.TagTypeId.ToString(),
                    Child = _context.Tags
                            .Where(y => y.TagTypeId == x.Key.TagTypeId)
                            .Select(y => new TagDto
                             {
                                 Title = y.Title,
                                 Measure = y.Measure.Title,
                                 Key = "Tag" + y.Id.ToString(),
                                 TagType = y.TagType.Title,
                                 Disabled = true,
                                 Child = null
                             })
                }).ToListAsync();

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

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