10101
10101

Reputation: 2412

Wrap linq query into if statement (different query options dependent on if statement)

I have created a WPF form with checkbox. If checkbox is checked I would like to compare data by two columns and if it is not checked only by one.

Below code is working fine but I am not sure I am doing it the best way possible?

Question: Is below code the right way to go for if statement? Are there better solutions?

Here is the code for if statement:

        IEnumerable<JoinedFIandSE> firstPart;
        IEnumerable<JoinedFIandSE> secondPart;

        if (MainWindow.CompareByTwoColumnsYes == true)
        {
            firstPart = from table1 in t2.AsEnumerable()
                        join table2 in t1.AsEnumerable()
                            on new { A = (string)table1["SNAME"], B = (string)table1["NAMEB"] } equals
                            new { A = (string)table2["SNAME"], B = (string)table2["NAMEB"] } into temp
                        from table2 in temp.DefaultIfEmpty()
                        select new JoinedFIandSE
                        {
                            NRO = (string)table1["NRO"],
                            NRO1 = table2 != null ? (string)table2["NRO"] : string.Empty,
                            SNAME = (string)table1["SNAME"],
                            NAMEA = (string)table1["NAMEA"],
                            NAMEB = (string)table1["NAMEB"],
                        };
        }
        else
        {
            firstPart = from table1 in t2.AsEnumerable()
                        join table2 in t1.AsEnumerable()
                            on (string)table1["SNAME"] equals (string)table2["SNAME"] into temp
                        from table2 in temp.DefaultIfEmpty()
                        select new JoinedFIandSE
                        {
                            NRO = (string)table1["NRO"],
                            NRO1 = table2 != null ? (string)table2["NRO"] : string.Empty,
                            SNAME = (string)table1["SNAME"],
                            NAMEA = (string)table1["NAMEA"],
                            NAMEB = (string)table1["NAMEB"],
                        };
        }

I am doing the same for another table and then

        var results = firstPart.Concat(secondPart);
        return results;

This code is inside public static IEnumerable<JoinedFIandSE> GetMyJoinedResult() method.

Upvotes: 0

Views: 56

Answers (1)

harpal
harpal

Reputation: 476

You can try this, it will just move your condition into join clause.

IEnumerable<JoinedFIandSE> firstPart;
IEnumerable<JoinedFIandSE> secondPart;


firstPart = from table1 in t2.AsEnumerable()
            join table2 in t1.AsEnumerable()
            on new { A = (string)table1["SNAME"], B =  
                           MainWindow.CompareByTwoColumnsYes ? (string)table1["NAMEB"]: "" } equals
            new { A = (string)table2["SNAME"], B = MainWindow.CompareByTwoColumnsYes ? (string)table2["NAMEB"]: "" } into temp
        from table2 in temp.DefaultIfEmpty()
        select new JoinedFIandSE
        {
            NRO = (string)table1["NRO"],
            NRO1 = table2 != null ? (string)table2["NRO"] : string.Empty,
            SNAME = (string)table1["SNAME"],
            NAMEA = (string)table1["NAMEA"],
            NAMEB = (string)table1["NAMEB"],
        };

There could be a more better solution for this.

Upvotes: 2

Related Questions