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
CodePudding user response:
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" });
CodePudding user response:
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