LINQ to use group by and starts with

Hi,

I have a datatable with 1 columns called names

I want to use a LINQ query to filter out the value with same first name i.e. and write it in a new datatable.

Example Datatable:
NAMES
AA AA
AA Bb
AA Cc

New datatable:
Aa

Any Idea?

Thanks in advance.

Hi,

How about the following?

newDt = dt.AsEnumerable.Select(Function(r) r("Names").ToString.Split({" "c})(0)).Distinct().Select(Function(x) newDt.LoadDataRow({x},False)).CopyToDataTable()

Sequence.xaml (8.6 KB)

Regards,

Hi @Yoichi ,
Instead of split can we use starts with?
Because some names have space and some don’t. Firstname and last name is written together.

Thanks in advance

Hi,

StartsWith method needs argument to check it. Which string do you want to check? Only “AA”? or do you have list of name?

Regards,

@Yoichi ,
There’s just one column and no other list.

Hi,

Firstname and last name is written together.

Generally, it’s difficult to separate firstname and lastname if there are no separator. Is there any rule to find it? (ex. Lastname always starts with capital. etc)

Regards,

@Yoichi , nothing specific.
Well, thank you so much for the input.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.