简体   繁体   English

C#从不同类的2个数组中创建集合。 使用Linq选择/从

[英]C# Creating Collection Out of 2 Arrays In different Class. Use Linq to Select/From

Here's my problem. 这是我的问题。 I have 2 arrays that are like this. 我有两个像这样的数组。

    string[] fruit1 = { "Apple", "Banana", "Orange", "Pear", "Strawberry", "Grape", "Kiwi" };
        string[] fruit2 = { "Peach", "Nectarine", "Banana", "Cherry" };

I was asked to create a second class. 我被要求创建第二堂课。 Add a "couple slightly different properties". 添加“夫妇的属性略有不同”。 Then use Linq to Select/From the first class to create a sorted collection in the second class that gets rid of the duplicate ("Banana"). 然后,使用Linq在第一类中选择/从第一类中创建一个已排序的集合,以消除重复项(“香蕉”)。

Here's what I've worked out, before being told that I needed to incorporate the second class, and the select/from. 这是我已经完成的工作,然后才被告知需要合并第二类和select / from。 The sorts are there as a part of the assignment. 排序是作业的一部分。 They work fine. 他们工作正常。 And this program works fine. 这个程序工作正常。 The only thing was, after I sent it, I was told the whole part of the second class. 唯一的事情是,在我发送完之后,我被告知第二节课的整个部分。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MergeArray
{
  class Program
  {
    static void Main(string[] args)
    {
        string[] fruit1 = { "Apple", "Banana", "Orange", "Pear", "Strawberry", "Grape", "Kiwi" };
        string[] fruit2 = { "Peach", "Nectarine", "Banana", "Cherry" };
        string[] fruit3 = new string[fruit1.Length + fruit2.Length];

        string temp;

        foreach (string i in fruit1)
        {
            for (int j = 0; j < fruit1.Length - 1; j++)
            {
                if (String.Compare(fruit1[j], fruit1[j + 1]) > 0)
                {
                    temp = fruit1[j];
                    fruit1[j] = fruit1[j + 1];
                    fruit1[j + 1] = temp;
                }
            }
        }
        foreach (string i in fruit2)
        {
            for (int j = 0; j < fruit2.Length - 1; j++)
            {
                if (String.Compare(fruit2[j], fruit2[j + 1]) > 0)
                {
                    temp = fruit2[j];
                    fruit2[j] = fruit2[j + 1];
                    fruit2[j + 1] = temp;
                }
            }
        }

        fruit1.CopyTo(fruit3, 0);
        fruit2.CopyTo(fruit3, names1.Length);

        Console.WriteLine(String.Join(Environment.NewLine, fruit3.Distinct().ToArray()));
        Console.ReadLine();

    }
}
}

So my question is. 所以我的问题是。 How would I pass these first 2 arrays into an outside class, so I could then use them to create the constructor (fruit3) in the outside class? 我如何将这些前两个数组传递给外部类,以便随后可以使用它们在外部类中创建构造函数(fruit3)? Then after that, return the new constructor to display the new merged/sorted constructor (without duplicates) of "Apple", "Banana", "Grape", "Kiwi", "Orange", "Pear", "Strawberry", "Cherry", "Nectarine", "Peach" ? 然后,返回新的构造函数,以显示“ Apple”,“ Banana”,“ Grape”,“ Kiwi”,“ Orange”,“ Pear”,“ Strawberry”,“樱桃”,“油桃”,“桃子”?

By using Concat , Distinct and OrderBy : 通过使用ConcatDistinctOrderBy

string[] fruit3 = 
    // concat these 2 togather
    fruit1.Concat(fruit2)
        // remove any duplicates
        .Distinct()
        // order them
        .OrderBy(x => x)
        // finally, converts IEnumerable<string> to string[]
        .ToArray();

As for using them outside of class , you need to move them outside of Main() method. 至于在类之外使用它们 ,则需要将它们移到Main()方法之外。 As local variable cannot be accessed anywhere other than within the declaring method, unless you pass it as via parameter. 因为局部变量不能在声明方法之外的任何地方访问,除非您通过参数传递它。 You also need to prefix them with the access modifier , public , as you need to access them outside of Program class, as such : 您还需要在它们前面加上访问修饰符 public ,因为您需要在Program类之外访问它们,例如:

class Program
{
    public string[] fruit1 = { "Apple", "Banana", "Orange", "Pear", "Strawberry", "Grape", "Kiwi" };
    public string[] fruit2 = { "Peach", "Nectarine", "Banana", "Cherry" };

    // ...
}

The public keyword is an access modifier for types and type members. public关键字是类型和类型成员的访问修饰符。 Public access is the most permissive access level. 公共访问是最宽松的访问级别。 There are no restrictions on accessing public members. 访问公共成员没有任何限制。

EDIT 2: There is many way that to have the Names class to do this. 编辑2:有很多方法可以使Names类执行此操作。 Here is an example : 这是一个例子:

class Program
{
    static void Main(string[] args)
    {
        string[] fruit1 = { "Apple", "Banana", "Orange", "Pear", "Strawberry", "Grape", "Kiwi" };
        string[] fruit2 = { "Peach", "Nectarine", "Banana", "Cherry" };
        string[] fruit3 = Names.UniqueSortedJoin(fruit1, fruit2);
    }
}

class Names
{
    public static string[] UniqueSortedJoin(string[] names1, string[] names2)
    {
        return names1.Concat(names2)
            .Distinct()
            .OrderBy(x => x)
            .ToArray();
    }
}

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

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