2

I have a list as

class Vachel
{
int id{get;set;}
int vachelid {get;set;}
string title {get;set;}
}

List of Vachel as

id  vachelid  title
1   2         bus
1   3         truck
2   4         cycle
2   5         bike
2   5         bick

And I want to convert List<Vachel> to List<Result>

class Result
{
int id{get;set;}
string vachelid {get;set;}
string title {get;set;}
}

And the result must be as

   id   vachelid    title
    1   2,3         bus,truck
    2   4,5         cycle,bike,bick

I tried as

List<Vachel> V = getList();
List<Result> R = null;
 R = V.GroupBy(x=> new{x.vachelid})
    .Select
       (
       x=> new Result
         {
         id  =x.Key.id,
         vachelid =x.FirstOrDefault().vachelid,
         title   =x.FirstOrDefault().title
         }
       ).ToList();

Here I know I want to put something instead of .FirstOrDefault().vachelid and .FirstOrDefault().title but I don't know what to do.

1 Answer 1

1
R = V.GroupBy(x => x.id)
     .Select(g => new Result(){
        id = g.Key,
        vachelid = string.Join(",", g.Select(x => x.vachelid).Distinct()),
        title = string.Join(",", g.Select(x => x.title).Distinct()),
     }).ToList();
Sign up to request clarification or add additional context in comments.

6 Comments

@user2641905: sorry, I am not following you, could you elaborate more?
plz have a look question again.
@user2641905: I have looked again but still did not get your point, please could you use English in decent if i want uniqes vales only seperet by?
here in List Vechle, for vecheleid = 5 there is two Title bike and bick and i want result as vecheleid = 5 and title = bike,bick from your code i get vechelid = 4,5,5 and Title = cycle,bike,bick but i only want vechelid = 4,5 and Title = cycle,bike,bick
@user2641905: got it, I have changed my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.