我如何追加LINQ查询到对方? [英] How do I append LINQ queries to each other?

查看:158
本文介绍了我如何追加LINQ查询到对方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这取决于他们选择什么筛选数据的形式。

I have a form that filters the data depending on what they select.

我想LINQ查询附加到彼此,这样最终的结果是什么,他们都在屏幕上选择。

I am trying to append linq queries to each other so that the end result is what they have selected on the screen.

下面是我的代码:

private void button_Search_Click(object sender, EventArgs e)
{
  using (var model = new SuburbanPortalEntities())
  {
    var qry = from logs in model.Logs
              select logs;

    Guid corpid;
    if (Guid.TryParse(textBox_CorporationGuid.Text, out corpid))
    {
      qry = from logs in model.Logs
                where logs.CorporationId == corpid
                select logs;
    }

    Guid tokenid;
    if (Guid.TryParse(textBox_TokenId.Text, out tokenid))
    {
      qry = from logs in model.Logs
            where logs.TokenId == tokenid
            orderby logs.LogDateTime descending 
            select logs;
    }

    if (checkBox_DisplayErrors.Checked)
    {
      qry = from logs in model.Logs
            where logs.IsException
            select logs;
    }

    if (checkBox_DisplayWarnings.Checked)
    {
      qry = from logs in model.Logs
            where logs.IsWarning
            select logs;
    }

    dataGridView1.DataSource = qry;


  }
}



我有没有运气。 QRY在过去是被显示在我的datagridview的。

I'm having no luck. The last qry in is what is displayed on my datagridview.

有人能告诉我什么,我做错了吗?

Can someone show me what I'm doing wrong?

由于!

推荐答案

这里发生的事情是,你每一次重新定义 QRY 从您的源数据。 QRY 的IEnumerable< T> 刚才一样日志,所以你应该能够取代像这样的代码:

What's happening here is that you're redefining qry from your source data each time. qry is an IEnumerable<T> just the same as logs, so you should be able to replace your code like so:

 var qry = model.Logs;

 if( // Condition )
    qry = qry.Where(x => x.CorporationId == corpId);

 if( // Another condition)
    qry = qry.Where(x => x.IsException);

目前安装结束, QRY 将所有选中其中,条款的组成和应出示只是你正在寻找的项目。

At the end of this setup, qry will be the composition of all the selected Where clauses and should produce just the items you are looking for.

这篇关于我如何追加LINQ查询到对方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆