0

I have this bit of code that joins 3 tables. In the end, the result is a list of users. The list that is returned usually contains records like "DG3\RONASS", but can also be DG4, DG5, etc. In this case, I'm replacing "DG3\" just to see how it works. How can I make it generic, so that it replaces the backslash and everything before it, without having to specify a string to replace like in this case?

 List<string> journalingUsers = Context.ResourceSetSet
                .Join(Context.ResourceItemJnSet, resourceSet => resourceSet.ID, resourceItemJn => resourceItemJn.ResourceSet_ID, (resourceSet, resourceItemJn) => new { ResourceSet = resourceSet, ResourceItemJn = resourceItemJn }).Where(join => join.ResourceSet.ToepassingsCode == toepassingsCode).Select(join => join.ResourceItemJn).ToList()
                .Join(Context.ResourceItemDotNetJnSet, resourceItemJn => resourceItemJn.ID, resourceItemDotNetJn => resourceItemDotNetJn.ID, (resourceItemJn, resourceItemDotNetJn) => new { ResourceItemJn = resourceItemJn, ResourceItemDotNetJn = resourceItemDotNetJn }).Select(join => join.ResourceItemDotNetJn).ToList()
                .Join(Context.ResourceItemWaardeJnSet, resourceItemDotNetJn => resourceItemDotNetJn.ID, resourceItemWaardeJn => resourceItemWaardeJn.ResourceItem_ID, (resourceItemDotNetJn, resourceItemWaardeJn) => new { ResourceItemDotNetJn = resourceItemDotNetJn, ResourceItemWaardeJn = resourceItemWaardeJn })
     .Select(join => join.ResourceItemWaardeJn.User.Replace(@"DG3\", ""))
     .Distinct().ToList();
3
  • You just want to return everything after the backslash?? Commented Aug 12, 2014 at 8:45
  • Right margin Overflow. Commented Aug 12, 2014 at 8:48
  • yes, that's it, DavidG. Commented Aug 12, 2014 at 8:48

3 Answers 3

3

Use the Substring function:

journalingUsers = journalingUsers.Select(s => s.Substring(s.IndexOf(@"\")+1));
Sign up to request clarification or add additional context in comments.

4 Comments

+1, more efficient and more redable than my string.split approach
Don't think you should have deleted yours though, it's still a valid answer.
Ok, i've undeleted it. Maybe it has some added value if OP needs to take/skip other parts.
Works like a charm, tks.
2

So you want to ignore the first part? A simple approach is using String.Split and Enumerable.Skip:

List<string> allButFirst = journalingUsers
    .Select(s => string.Join("\\", s.Split('\\').Skip(1)))
    .ToList();

3 Comments

It's not a path, just Domain\User so you don't need to Join again. Split(...)[2] is enough.
@HenkHolterman: You've lost me. OP doesn't mention Domain\User or that it contains three tokens.
Yeah, should have been [1]
-1

This question on #Stackoverflow might help you out, check it out?

Link to Question on stackoverflow

Comments

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.