簡體   English   中英

從C#列表向LINQ查詢結果發送新列表

[英]Send a new list to the result of the LINQ query from the list for C#

我正在從創建的類創建通用列表,然后輸入所需的數據。

    public class GroupList
    {
        public int pertt { get; set; }
        public int pips { get; set; }
        public int[] iVals;

        public GroupList(int PerTT , int Pips , int[] iValues)
        {
            this.pertt = PerTT;
            this.pips = Pips;
            iVals = iValues;
        }

        public override string ToString()
        {
            return $"PerTT: {pertt} Pips: {pips} Values: {string.Join(", ", iVals)}";
        }
    }

我想將數據輸入到派生自該類的mydatalist列表中,並將LINQ查詢發送到Mylinqresult Generic列表,並正確打印它並執行數學運算。

static void Main(string[] args)
{
    List<GroupList> myDataList = new List<GroupList>
    {
        new GroupList(15, 65, new[] {3, 9, 21, 1, 56}),
        new GroupList(15, 65, new[] {13, 19, 121, 11, 156}),
        new GroupList(10, 19, new[] {23, 29, 221, 12, 562}),
        new GroupList(10, 21, new[] {33, 93, 213, 13, 356}),
        new GroupList(21, 9, new[] {43, 49, 421, 41, 456}),
        new GroupList(21, 19, new[] {35, 95, 216, 17, 56})
    };

    List<GroupList> myLinqResult = new List<GroupList>();
    myLinqResult = from x in myDataList
                   where x.iVals[] >  65
                   select x;
}

以這種方式鍵入查詢時,出現編譯錯誤。

可以基於Int32數組中給出的參數查詢查詢,也可以通過將結果發送到相同的格式列表以打印並執行數據的數學運算來訪問查詢。

如果要選擇其數組中的項超過65的所有GroupList對象,則可以執行以下操作:

List<GroupList> myLinqResult = myDataList
    .Where(item => item.iVals.Any(i => i > 65))
    .ToList();

否則,如果您要選擇所有超過65的項目(無論它們屬於哪個GroupList ),都可以執行以下操作:

List<int> allItemsOver65 = myDataList
    .SelectMany(item => item.iVals)
    .Where(i => i > 65)
    .ToList();

最后,如果要選擇與原始匹配的 GroupList項,但在其iVal數組中僅包含65個以上的項,則可以執行以下操作:

List<GroupList> myLinqResult = myDataList
    .Where(item => item.iVals.Any(i => i > 65))
    .Select(item => 
        new GroupList(item.pertt, item.pips, item.iVals.Where(i => i > 65).ToArray()))
    .ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM