2

I'm new to linq. I have a string having the following format

code:description;code2:description2;code3:description3... etc.

Records are separated with ; character and each record has 2 fields separated with : character.

I'm writing a linq query to extract a list of objects having as fields code and description. I have written the following query which seems to produce correct results, but I was wondering if there is a better or more correct way to do it.

var objects =
    from objString in recsString.Split(';')
    let obj = objString.Split(':')
    select new {
        Code = obj[0].Trim(),
        Description = obj[1].Trim()
    };
4
  • Is your "object" defined as its own class somewhere in your project. If so, you could call a constructor in your select statement. IE - select new Record(code, description); Commented May 22, 2013 at 14:25
  • No it's anonymous, I will create a class if I see I need it, but for now it's enough. Commented May 22, 2013 at 14:25
  • This will not work if you have : or ; in code or description, or if any entry is empty or half-empty. Other than that its fine. Commented May 22, 2013 at 14:28
  • @DavidS. Fortunately there are not such cases. Commented May 22, 2013 at 14:31

2 Answers 2

6

That's perfectly fine, the only observation I would make though is that you remove empty entries by using the StringSplitOptions:

var objects =
from objString in recsString.Split(';', StringSplitOptions.RemoveEmptyEntries)
let obj = objString.Split(':', StringSplitOptions.RemoveEmptyEntries)
select new {
    Code = obj[0].Trim(),
    Description = obj[1].Trim()
};

If you think that there could be missing information, you could also be extra safe and null check the results:

var objects =
from objString in recsString.Split(';', StringSplitOptions.RemoveEmptyEntries)
let obj = objString.Split(':', StringSplitOptions.RemoveEmptyEntries)
select new {
    Code = obj.Any() ? obj[0].Trim() : string.Empty,
    Description = obj.Count > 1 ? obj[1].Trim() : string.Empty
};
Sign up to request clarification or add additional context in comments.

1 Comment

Ok then! Thanks for the extra recommendations.
3

What you're doing is just fine, here is how you'd write it using lambdas:

string objString = "code:description;code2:description2;code3:description3";

Dictionary<string, string> results =
    objString.Split(';')
             .Select(x => x.Split(':'))
             .ToDictionary(key => key[0], value => value[1]);

// And now you have a nice little dictionary
foreach (var r in results)
    Console.WriteLine("{0}:{1}",r.Key, r.Value);

Or of course:

    var results = objString.Split(';')
                           .Select(x => x.Split(':'))
                           .Select(x => new {Code = x[0], Description = x[1]});

    foreach (var r in results)
        Console.WriteLine("{0}:{1}",r.Code, r.Description);

2 Comments

Nice approach. Objects list suits better my needs for now.
Sure, I though a different prospective might be useful :)

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.