簡體   English   中英

如何在Linq查詢中將select多個元素放入數組?

[英]How to select multiple elements into array in Linq query?

凈核心應用。 我的應用程序中有以下查詢

 var result  = sourceProposal.Quotes
      .Where(x=>x.QuotationId == sourceQuoteId)
      .FirstOrDefault()
      .QuoteLines.Select(x=>(x.Quantity,x.WtgType)).ToArray();

這會產生兩個數組元素,例如

0 element   1, "string1"
1 element   2, "string2"

我期待的是

(int[] sourceQuantity, string[] destinationTurbineType) = sourceProposal.Quotes
           .Where(x=>x.QuotationId == sourceQuoteId)
           .FirstOrDefault()
           .QuoteLines.Select(x=>(x.Quantity,x.WtgType)).ToArray();

我想復制到具有int[] sourceQuantitystring[] destinationTurbineType的元組 這段代碼不起作用並且拋出錯誤不包含析構函數的定義並且沒有可訪問的擴展方法 Descontruct 接受類型為int(sourceQuantity, string destinationTurbineType)[]的第一個參數int(sourceQuantity, string destinationTurbineType)[]

有人可以幫我將值復制到sourceQuantitydestinationTurbineType 任何幫助,將不勝感激。 謝謝

Select<TSource,TResult>返回由選擇器 ( IEnumerabe<TResult> / IQueryable <TResult> ) 返回的類型的可枚舉/可查詢類型。

如果你想用 LINQ 實現這個,你可以使用Aggregate

// note that sourceQuantity and destinationTurbineType would be lists, not arrays
var (sourceQuantity, destinationTurbineType) = sourceProposal.Quotes
    .Where(x=>x.QuotationId == sourceQuoteId)
    .FirstOrDefault()
    .QuoteLines
    .Aggregate((ints: new List<int>(), strs: new List<string>()), (aggr, curr) =>
    {
        aggr.ints.Add(curr.Quantity);
        aggr.strs.Add(curr.WtgType);
        return aggr;
    });

或者只使用簡單for循環並將數據復制到目標 arrays(可能移動到某種擴展方法)。 沿着這條線的東西:

var quoteLines = sourceProposal.Quotes
    .Where(x=>x.QuotationId == sourceQuoteId)
    .FirstOrDefault()
    .QuoteLines; // assuming it is materialized collection with indexer like an array or list
int[] sourceQuantity = new int[quoteLines.Length]; // or Count
string[] destinationTurbineType = new string[quoteLines.Count()];
for(int i = 0; i < quoteLines.Length; i++)
{
   var curr = quoteLines[i];
   sourceQuantity[i] = curr.Quantity;
   destinationTurbineType[i] = curr.WtgType;
}

目前沒有內置的 LINQ 方法來執行此操作。 但是您可以編寫自己的擴展方法。 類似於以下內容:

public static class EnumerableExtensions
{
    public static (TFirst[] xs, TSecond[] ys) Unzip<TFirst, TSecond>(this IEnumerable<(TFirst, TSecond)> zipped)
    {
        var xs = new List<TFirst>();
        var ys = new List<TSecond>();
        foreach (var (x, y) in zipped)
        {
            xs.Add(x);
            ys.Add(y);
        }
        return (xs.ToArray(), ys.ToArray());
    }
}

var (xs, ys) =
    new[] { 1, 2, 3 }
    .Zip(new[] { "a", "b", "c" })
    .Unzip();

Console.WriteLine(string.Join(", ", xs)); // 1, 2, 3
Console.WriteLine(string.Join(", ", ys)); // a, b, c

或者在您的示例中,您可以使用:

(int[] sourceQuantity, string[] destinationTurbineType) = sourceProposal.Quotes
           .Where(x=>x.QuotationId == sourceQuoteId)
           .FirstOrDefault()
           .QuoteLines.Select(x=>(x.Quantity,x.WtgType)).Unzip();

暫無
暫無

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

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