Join

S

shapper

Hello,

I have a List<Role>, Roles, where Role has two properties: Type and
Description. Then I have: string [] MyRoles.

I need to define a CSV list created by all Desciptions of the
List<Role> which type is contained in string [] MyRoles.

string RolesCSV = Roles.Join(",", r => MyRoles.Contains(r.Type));

I get:
String[] does not contain a definition Contains.

.... But it is in the list as I right ...

What am I doing wrong?

Thanks,
Miguel
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

I have a List<Role>, Roles, where Role has two properties: Type and
Description. Then I have: string [] MyRoles.

I need to define a CSV list created by all Desciptions of the
List<Role> which type is contained in string [] MyRoles.

string RolesCSV = Roles.Join(",", r => MyRoles.Contains(r.Type));

I get:
String[] does not contain a definition Contains.

... But it is in the list as I right ...

What am I doing wrong?

Thanks,
Miguel

Hi,

IIRC Array does not have a member Contains, you cuold use Find
instead:
Roles.Select( (r)=> MyRoles.Find(r)!=null)
 
J

Jeroen Mostert

shapper said:
I have a List<Role>, Roles, where Role has two properties: Type and
Description. Then I have: string [] MyRoles.

I need to define a CSV list created by all Desciptions of the
List<Role> which type is contained in string [] MyRoles.

string RolesCSV = Roles.Join(",", r => MyRoles.Contains(r.Type));

I get:
String[] does not contain a definition Contains.
Even if there were, you code makes no sense. .Contains() returns a boolean,
the .Join() method you're invoking doesn't take two arguments, and
..Description isn't anywhere.

Try

string RolesCSV = string.Join(",", (from r in Roles where
MyRoles.Contains(r.Type) select r.Description).ToArray());

If that still fails to compile, you need "using System.Linq;" at the top.
 
S

shapper

shapper said:
I have a List<Role>, Roles, where Role has two properties: Type and
Description. Then I have: string [] MyRoles.
I need to define a CSV list created by all Desciptions of the
List<Role> which type is contained in string [] MyRoles.
string RolesCSV = Roles.Join(",", r => MyRoles.Contains(r.Type));
I get:
String[] does not contain a definition Contains.

Even if there were, you code makes no sense. .Contains() returns a boolean,
the .Join() method you're invoking doesn't take two arguments, and
.Description isn't anywhere.

Try

   string RolesCSV = string.Join(",", (from r in Roles where
MyRoles.Contains(r.Type) select r.Description).ToArray());

If that still fails to compile, you need "using System.Linq;" at the top.

I am trying to use the following sintax:

string b = string.Join(", ", Roles.Where(r => MyRoles.Contains
(r.Type.ToString()) == true).Select(r => r.Description).ToArray());

Again I keep having the error:
'string[]' does not contain a definition for 'Contains' and the best
extension method overload 'MyApp.Mvc.ViewDataPaper.Contains
(System.Web.Mvc.ViewDataDictionary, System.Type)' has some invalid
arguments

And I can't find the method Find for MyRoles ... only Contains.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Generics 7
Sring[] to List<MyEnumeration> 5
Order List Of Anonymous Type 1
Update Object 5
Linq > Contains 2
Update Roles 1
Create List in Linq 4
Remove from List 2

Top