-1

Hello I have this problem where I have this classes:

    class numberOne 
    {
      public long ID {get; set;}
      public string Name {get; set;}
      public string Names {get; set;}
    }
    class numberTwo
    {
     public long[] ID {get; set;}
     public string Name {get; set;}
    }
  

they will be list after.

So lets say that the data of those lists are like this

   List<numberOne> numberOne = new List<numberOne>();
             numberOne.Add(new numberOne() { ID = 1234 });
             numberOne.Add(new numberOne() { ID = 1334 });
             numberOne.Add(new numberOne() { ID = 1434 });
             numberOne.Add(new numberOne() { ID = 1568 });
    List<numberTwo> numberTwo = new List<numberTwo>();
             numberTwo.Add(new numberTwo() { ID[] = 1234,1334 Name = "sam" });         
             numberTwo.Add(new numberTwo() { ID[] = 1434, Name = "paul" });
             numberTwo.Add(new numberTwo() { ID[] = 1434, Name = "john" });
             numberTwo.Add(new numberTwo() { ID[] = 1568, Name = "john" });

I need that the the data can be show like this:

ID  | Name | Names
1234|sam   |sam
1334|sam   |sam
1434|paul  |paul, john
1434|john  |paul, john
1568|john  |john

So far I have this of code:

foreach(var items in numberOne.ToList())
         {
items.Name = numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name).Distinct();
items.Names = string.Join(",", numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name).Distinct);

in this line "items.Name = numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name).Distinct(); " is where I receive the error CS0029

darksaky
  • 1
  • 3
  • Some of us don't know by heart what error CS0029 is. Show the entire error message including the types. – Gert Arnold Sep 16 '21 at 18:03
  • In C#, types are very important. What is the return type of your `Distinct` method call? What is the type of `items.Name`? What did you intend to assign to `items.Name`? – NetMage Sep 16 '21 at 18:39

2 Answers2

0

Name is a string and you pass an IEnumerable<string> to a string.

you should change your code to this:

foreach (var items in numberOne.ToList())
{
    items.Name = string.Join(",", numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name).Distinct());
    items.Names = string.Join(",", numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name).Distinct());
}

Finally your code should be like this:

List<numberOne> numberOne = new List<numberOne>();
numberOne.Add(new numberOne() { ID = 1234 });
numberOne.Add(new numberOne() { ID = 1334 });
numberOne.Add(new numberOne() { ID = 1434 });
numberOne.Add(new numberOne() { ID = 1568 });
List<numberTwo> numberTwo = new List<numberTwo>();
numberTwo.Add(new numberTwo() { ID = new long[] { 1234, 1334 }, Name = "sam" });
numberTwo.Add(new numberTwo() { ID = new long[] { 1434 }, Name = "paul" });
numberTwo.Add(new numberTwo() { ID = new long[] { 1434 }, Name = "john" });
numberTwo.Add(new numberTwo() { ID = new long[] { 1568 }, Name = "john" });
foreach (var items in numberOne.ToList())
{
    items.Name = string.Join(",", numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name));
    items.Names = string.Join(",", numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name));
    Console.Write(items.ID + "\t");
    Console.Write(items.Name + "\t");
    Console.WriteLine(items.Names);
}

Result:

1234    sam     sam
1334    sam     sam
1434    paul,john       paul,john
1568    john    john
Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
0

try this


    foreach (var items in numberOne.ToList())
    {
        items.Name = numberTwo.Where(a => a.ID.Any(b => b == items.ID)).Select(a => a.Name)
.Distinct().FirstOrDefault();
        items.Names = string.Join(",", numberTwo.Select(a => a.Name).Distinct().ToList());
    }

after fixing numberTwo init

List<numberTwo> numberTwo = new List<numberTwo>();

    numberTwo.Add(new numberTwo() { ID = new long[] { 1234 }, Name = "sam" });
    numberTwo.Add(new numberTwo() { ID = new long[] { 1334 }, Name = "paul" });
    numberTwo.Add(new numberTwo() { ID = new long[] { 1434 }, Name = "john" });
    numberTwo.Add(new numberTwo() { ID = new long[] { 1568 }, Name = "john" });
Serge
  • 40,935
  • 4
  • 18
  • 45