0

I have an array of object storing some accounts.

12345631
L1234512
P12345
234556
19090912
J123456

Using Linq and .Net 3.5, I want to create a csv string for accounts starting with L or P and is of 6 characters. Following code creates a csv string but how to add the filter?

string[] accounts = accountList.Select(c => c.acctnbr).ToArray();
string csv = string.Join(",", accounts);

The output should be L12345,P12345.

3
  • L1234512 is more than 6 characters, do you want the account number truncated to six characters or is it just a typo? Commented Apr 21, 2011 at 18:54
  • just a typo. my bad :( I am confused now on which solution is efficient - Cybernate's or Romanarmy's? or, is the same? Commented Apr 21, 2011 at 19:04
  • Same solution, different syntax Commented Apr 21, 2011 at 19:11

3 Answers 3

4

Try this:

string[] accounts = accountList
.Where
(
    a=> (a.acctnbr.StartsWith("L") || a.acctnbr.StartsWith("P")) 
        && 
        (a.acctnbr.Length == 6)
)
.Select(c => c.acctnbr)
.ToArray();
string csv = string.Join(",", accounts);
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I'd do, but I think it should be a.acctnbr.StartsWith(...) to match the original code.
2

In case you't interested, here's a version using the query syntax instead as well

var accounts = from a in accountList
               where (a.acctnbr.StartsWith("L") || a.acctnbr.StartsWith("P")) 
                  && (a.acctnbr.Length == 6)
               select a.acctnbr;

var csv = String.Join("," accounts.ToArray());

Based on the comment you added regarding which solution is better, mine or Cybernate. The solutions are the same, C# let's you write queries in two ways. You can take a look at LINQ Query Syntax versus Method Syntax (C#) for more information.

Relevant bit from ducumentation (though I would strongly urge you to read up more on Linq)

In general, we recommend query syntax because it is usually simpler and more readable; however there is no semantic difference between method syntax and query syntax. In addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls.

1 Comment

I just implemented your solution and was more cleaner and simple as you mentioned. And, thanks for the link and the description. It helps the newbies like me. I will definitely read more on Linq.
0
string[] accounts = accountList.Select(c => c.acctnbr).ToArray();
IEnumerable<string> namesWithFourCharacters = from name in accounts 
                                              where name.StartsWith("L")
                                              select name;

Hope this helps.

2 Comments

You code would do two passes over the collection, first turning it into an array then doing the filtering (where as it can just as easily be done in one). It also doesn't address his part of the question: starting with L or P and is of 6 characters.
May want to include that bit of information, based on the question I'm guessing he doesn't have a very firm grasp of Linq.

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.