繁体   English   中英

如何创建具有属性列表之一的行列表

[英]How can I create a list of rows that have one of a list of properties

我要执行一个困难的LINQ查询。 这是我所拥有的:

IJapaneseDictionaryEntry对象的列表,如下所述。 在其中是IKanji对象。

public interface IJapaneseDictionaryEntry
{
    int Sequence { get; }
    IEnumerable<IKanji> Kanjis { get; }
    IEnumerable<IReading> Readings { get; }
    IEnumerable<ISense> Senses { get; }
}

每个对象都包含IKanji对象的列表

public interface IKanji
{
    string Text { get; }
    IEnumerable<KanjiInformation> Informations { get; }
    IEnumerable<Priority> Priorities { get; }
}

我正在使用此查询:

var b = entries.SelectMany(x => x.Kanjis)
         .Where(x => x.Priorities.Any())
         .Select(x => new { x.Text, x.Priorities });

但我不想选择.Any(),而是选择那些一直包含Frequency1或Frequency2或Frequency3属性的行,一直到Frequency24

有什么办法可以做到这一点,并将.Any替换为将表示Frequency1,频率2,频率3 ...频率24中的任何一个的构造。

我添加了一个图像来显示优先级。 代码值从nf1到nf24

在此处输入图片说明

在此处输入图片说明

作为参考,下面是带有一些采样频率的优先级类别:

namespace Wacton.Desu
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    using Wacton.Tovarisch.Enum;

    public class Priority : Enumeration
    {
        public static readonly Priority Newspaper1 = new Priority("Newspaper1", "news1");
        public static readonly Priority Newspaper2 = new Priority("Newspaper2", "news2");
        public static readonly Priority Frequency1 = new Priority("Frequency1", "nf01");
        public static readonly Priority Frequency2 = new Priority("Frequency2", "nf02");
        public static readonly Priority Frequency3 = new Priority("Frequency3", "nf03");
        public static readonly Priority Frequency4 = new Priority("Frequency4", "nf04");
        public static readonly Priority Frequency5 = new Priority("Frequency5", "nf05");

        public string Code { get; }

        private static int counter;
        public Priority(string displayName, string code)
            : base(counter++, displayName)
        {
            this.Code = code;
        }
    }
}

我从发布的其他片段中假设,每个优先级都有一个代码,每个频率优先级都有一个类似“ nf ...”的代码,因此您可以在Any中放入布尔测试:

...Priorities.Any(pri => pri.Code.StartsWith("nf"))

如果有高于24的频率,而您想排除它们,则可能需要使用布尔测试来变得更可爱。 可能足以:

...Priorities.Any(pri => pri.Code.StartsWith("nf") && (pri.Code.Length == 3 || pri.Code <= "nf24"))

它在很大程度上取决于该库的结构以及其随着时间的变化。 设计决策供您而非我们做出,但这应该给您一些想法

暂无
暂无

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

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