简体   繁体   English

如何在Linq查询中将select多个元素放入数组?

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

Net core application.净核心应用。 I have below query in my application我的应用程序中有以下查询

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

This yields in two array elements such as这会产生两个数组元素,例如

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

What I am expecting is我期待的是

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

I want to copy to tuple which has int[] sourceQuantity , string[] destinationTurbineType this piece of code is not working and throwing error does not contain definition for destructor and no accessible extension method Descontruct accepting first argument of type int(sourceQuantity, string destinationTurbineType)[] .我想复制到具有int[] sourceQuantitystring[] destinationTurbineType的元组 这段代码不起作用并且抛出错误不包含析构函数的定义并且没有可访问的扩展方法 Descontruct 接受类型为int(sourceQuantity, string destinationTurbineType)[]的第一个参数int(sourceQuantity, string destinationTurbineType)[]

Can someone help me to copy values to sourceQuantity and destinationTurbineType .有人可以帮我将值复制到sourceQuantitydestinationTurbineType Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

Select<TSource,TResult> returns enumerable/queryable of the type returned by selector ( IEnumerabe<TResult> / IQueryable <TResult> ). Select<TSource,TResult>返回由选择器 ( IEnumerabe<TResult> / IQueryable <TResult> ) 返回的类型的可枚举/可查询类型。

If you want to achieve this with LINQ you can use Aggregate :如果你想用 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;
    });

Or just use simple for loop and copy data to destination arrays (possibly move to some extension method).或者只使用简单for循环并将数据复制到目标 arrays(可能移动到某种扩展方法)。 Something along this lines:沿着这条线的东西:

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;
}

Currently there is no built-in LINQ method to do this.目前没有内置的 LINQ 方法来执行此操作。 But you could write your own extension method.但是您可以编写自己的扩展方法。 Something like the following:类似于以下内容:

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

Or in the case of your example, you could then use:或者在您的示例中,您可以使用:

(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