Random Character With LINQ


Just now, I checked my forgotten stackoverflow account. I looked on some common questions and upvoted the good ones. Then I stumbled to this great answer. Why it was so great? Because it solves common problem, just creating random characters, but using such advance thing. I mean people usually just use Random and create character. This awesome man, using LINQ, giving range of characters to create the random set of character. If you are too lazy to see that, you can see the code below.

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var result = new string(
    Enumerable.Repeat(chars, 8)
              .Select(s => s[random.Next(s.Length)])
              .ToArray());

all credits goes to dtb

Spoiled Programmer


Hi! This blog is getting lame and its contents are starting to rust.

Still with, me, a web developer, also an application developer, the owner of this blog, Aditya Situmeang. Oh, before we’re getting to the topic of how spoiled I am as a programmer, I would like to announce that I have made a contribution to world music by uploading my own singing recording to soundcloud. You can check it in https://soundcloud.com/aditya-yedija-situmeang

You all, the .NET developers must have known LINQ. For some years LINQ has provided us an easy and fast way to manipulate with our lists of objects. Same here happened to me. As I have made some applications and webs, I stumble upon a client which server are so old, it did not support DBML. Whoa?! How can it be? I don’t know either but about hundreds of hours of testing and about fifty testing data are wasted because they can’t use DBML.

After knowing that their server did not support the technology (which is known after so many failure and they refused to be blamed), I offer them to use native way of using SqlConnection and SqlCommand objects. They agree and it is my job again to repair my codes.

The most surprising thing is that I forgot how to use the native way. The SqlConnection way to connect to database and use SqlCommand to execute your TSQL (TSQL because I use SQLServer of course) had became a new thing for me, again. I searched and what I got are silly tutorials in the internet. That means I did not search really well. Then I remember http://www.dotnetperls.com/ and they have the material that I needed. Fortunately everything is going well, though it took me quite a long time to solve the same problem but with native way.

From the story, I know that I have forgotten what was told to me long time ago. “A professional knows the basic and lived the basic. You must not google for something that you’ve lived on.” I have lived that basic way of connecting to database and creating my own connection string. But then, I forgot that way because I live too long in easy way such as LINQ, DBML, plugins from VS, etc. Just like Riddick had been blindsided by the glamour and power when he was the leader of the Necromongers, we programmers, developers, maybe we have been blindsided too, by so many plugins and easy tools that we use.

.NET – Linq, foreach, and eyesore


Hello all!

I was building an asp site. So, I got into a feature that I can use either linq and foreach. What is about the eyesore? You’ll get it later.

Well, the feature that I wanted to make is to filter a list based on a criteria. Let’s just say that I had a big list of products. The filter would be “include item”, “exclude item”, and “I don’t care if that exist”. The filter was based on the product’s category.

First, I created a function that would receive all of the products. From that list, I created a smaller list that only contained all included items. I used linq to include them all! Simple and cost me a small amount of LOC. Then from that all included products, I removed all those that doesn’t included inside it.

Well, for once or twice it works fine. All sorted well. Then I met a process when I tested my small function. I tried to exclude all. What I got when I’m using linq is error. Error, error everywhere! Exceptions, exceptions everywhere! Of course I didn’t manage to catch it through. Why should I? I was testing, I wanted to know the location of possible exceptions first.

Well, that error confused me. Why? Even though I wrote linq in multiple lines, it treated as one error. Plus, I had to read a long linq statement – yes, it was so damn long about three lines with full width on screen. It gives me sore eyes! Hell! I read through out the error message, then linq, and repeat, and poof! I surrender.

Then I decided to take the linq that exclude the products into a foreach. Pheeew! I feel so glad when I can track the bug easily. What I got is the list got emptied by the time is still searching on the list. Or, some index were skipped just like that because I remove the element. Whoa! I realized that I was making a huge mistake there.

So, in the end, I created a temporary list that contains the same element as the included items list. But I was not just assign it and done. First I instantiate an empty temporary list, then I add all included items into it. Later on, I use foreach to search on the included items, but I delete the item on the temporary list.

It sounds so complex, why do I have to use foreach then a temporary list for such simple job? Why not includedList.RemoveAll(criteria)?

Because I need a good way to track things down. Also, I consider a good way to extend it in the future, so the method may be used by other class. Simple and easy to manage, debug, but cost more LOC, that’s using foreach. Well, you may say even more memories and more time. But using linq, it is hard to track things down.

I still recommend you to use linq anyway, it is a great way to get things done, but when you stuck up, use my suggestion and way then 🙂

I’ll post the code at about 12.00(GMT+7) so you could see the difference. Happy coding!

LINQ:

productList.RemoveAll(o => o.Fields["Category"].Value.Contains(criteria));
// it's just a short code, yes, but that criteria went so long and contains private data, I can't post it here.

using foreach:

productList = RemoveFromList(filter, productList);

private List<Item> RemoveFromList(string filter, List<Item> list)
{
    List<Item> temp = new List<Item>();
    temp.AddRange(list); // the temporary list
    foreach (Item single in list) // I loop on the main list
    {
        foreach (Item child in single.Children)
        {
            if (!child.Name.StartsWith("xxx") && !child.Name.StartsWith("yyy")) // sorting criteria
            {
                temp.Remove(single);
            }
        }
    }
    return temp;
}