esac
esac

Reputation: 24695

How do I combine multiple linq queries into one results set?

I have a multi-select checkbox. Depending on which one is checked, I want to combine the results into a single query. Sort of like:

if (Checkbox1.Checked)
{
    var query1 = from t in table1 ...
}

if (Checkbox2.Checked)
{
    var query2 = from t in table2 ...
}

DataGridView1.DataSource = query1.Union(query2); // obviously doesnt
      // work since query1 and query2 are not defined in this scope.

Any idea how to combine these selectively?

Upvotes: 7

Views: 17207

Answers (4)

Richard Anthony Hein
Richard Anthony Hein

Reputation: 10650

Using Rx you can do something like this:

public partial class _Default : System.Web.UI.Page
{
    IEnumerable<int> table1;
    IEnumerable<int> table2;

    protected void Page_Load(object sender, EventArgs e)
    {
        table1 = Enumerable.Range(0, 10);
        table2 = Enumerable.Range(10, 10);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        var query = 
            Observable.If(() => CheckBox1.Checked,
                (from t in table1 select t).ToObservable(), Observable.Empty<int>())
            .Concat(
                Observable.If(() => CheckBox2.Checked,
                (from t in table2 select t).ToObservable(), Observable.Empty<int>())
            );

        query.Subscribe(i => Response.Write(i));
    }
}

Upvotes: 1

Ben M
Ben M

Reputation: 22512

Assuming the queries are of the same type, you could define the queries outside of the conditional statements.

First, a helper method that creates an empty enumerable of the same type as the parameter:

static IEnumerable<T> CreateEmptyEnumerable<T>(IEnumerable<T> templateQuery)
{
    return Enumerable.Empty<T>();
}

Then, the new code:

var query1 = from t in table1 ...
var query2 = from t in table2 ...
var finalQuery = CreateEmptyEnumerable(query1);

if (Checkbox1.Checked)
{
    finalQuery = query1;
}

if (Checkbox2.Checked)
{
    finalQuery = finalQuery.Union(query2);
}

DataGridView1.DataSource = finalQuery.ToList(); // to avoid re-enumeration

This performs just fine because the queries aren't actually executed until they're enumerated over, as in the call to ToList().

Upvotes: 10

Antony Scott
Antony Scott

Reputation: 22006

I just tried this in a console application, as I was curious, and it just worked....

class Program
{
    private class Data1
    {
        public int Code1 { get; set; }
        public string Value1 { get; set; }
    }

    private class Data2
    {
        public int Code2 { get; set; }
        public string Value2 { get; set; }
    }

    static void Main(string[] args)
    {
        var data1 = new[] { new Data1 { Code1 = 1, Value1 = "one" }, new Data1 { Code1 = 2, Value1 = "two" }, new Data1 { Code1 = 3, Value1 = "three" } };
        var data2 = new[] { new Data2 { Code2 = 101, Value2 = "aaa" }, new Data2 { Code2 = 102, Value2 = "bbb" }, new Data2 { Code2 = 103, Value2 = "ccc" } };

        var query1 = from d1 in data1 select new { code = d1.Code1, text = d1.Value1 };
        var query2 = from d2 in data2 select new { code = d2.Code2, text = d2.Value2 };

        var datasource = query1.Union(query2);

        foreach (var d in datasource)
        {
            Console.WriteLine(d);
        }
    }
}

However, if I change the fieldnames in the anonymous types to be different I get a compiler error, so it looks like the key is to have the names the same in your anonymous types. Or just have the result in a defined type :)

Upvotes: 0

Frank
Frank

Reputation: 2648

If you would not use var but a known type, then you could initialize query1 and query2 via

Enumerable.Empty<T> in case of the checkbox not being checked.

Upvotes: 0

Related Questions