如何使用 Linq2SQL 在 ASP.NET 和 C# 中查询未知数量的 where 参数?

发布于 2024-09-15 23:52:21 字数 1976 浏览 5 评论 0原文

我有一个 GridView,它使用 LinqDataSource 来填充表 Tickets 中的数据。 LinqDataSource 具有 AutoGenerateWhereClause="True" ,以便可以像这样动态构造 where 子句:

<asp:LinqDataSource ID="dsGridView" runat="server" AutoGenerateWhereClause="True"  
     ontextTypeName="TicketsDataContext" TableName="Tickets" OrderBy="ID Descending"  
     EnableDelete="True" OnSelecting="dsGridView_Selecting">  
    <WhereParameters>  
        <asp:SessionParameter Name="AssignedTo" SessionField="user"/>  
        <asp:Parameter Name="Department" DefaultValue="" />  
        <asp:Parameter Name="Category" DefaultValue="" />  
    </WhereParameters>  
</asp:LinqDataSource>

Where 参数与 gridview 标题上的下拉列表过滤器一起使用。它们可以为 null,以便 dsGridView 将返回所有记录。

我的 gridview 启用了分页。

表 Ticket 有一个名为 TimeSpent 的字段。我想计算所有过滤的工单的总 TimeSpent 并将其显示在页脚上。

我可以在 gridView_DataBound 上使用 Linq2SQL 来查询所有票证的 TimeSpent。但是,我很困惑如何在过滤 gridview 时获取总 TimeSpent 。

我尝试从 LinqDataSourceSelectEventArgs.Result 获取票证,但它只返回 gridview 当前页面的总 TimeSpent,而不是整个表。

问题是,我不知道 Selecting 事件中会出现多少个Where 参数。 Department 可以为 null 并且不会显示在WhereParameters 中,Category 也可以为空。

像这样的事情:

TicketsDataContext db = new TicketsDataContext();  
var query = from ticket in db.Tickets select ticket;  
foreach (var param in dsGridView.WhereParameters  
{  
    if (!string.IsNullOrEmpty(param.Value))  
        query.query.Where(...)  
}

当然行不通。有什么想法可以解决这个问题吗?提前致谢!

更新:我最终在 OnSelected 事件中重用了从 dsGridView 返回的数据,如下所示:

protected void dsGridView_Selected(Object sender, LinqDataSourceStatusEventArgs e)  
{  
    var totalTime = (e.Result as List<Ticket>).Sum(t => t.TimeSpent);  
    grvTickets.Columns[7].FooterText = "Sum: " + totalTime.ToString();  
}

I have a GridView using LinqDataSource to populate data from table Tickets. The LinqDataSource has AutoGenerateWhereClause="True" so that the where clause can be constructed dynamically like this:

<asp:LinqDataSource ID="dsGridView" runat="server" AutoGenerateWhereClause="True"  
     ontextTypeName="TicketsDataContext" TableName="Tickets" OrderBy="ID Descending"  
     EnableDelete="True" OnSelecting="dsGridView_Selecting">  
    <WhereParameters>  
        <asp:SessionParameter Name="AssignedTo" SessionField="user"/>  
        <asp:Parameter Name="Department" DefaultValue="" />  
        <asp:Parameter Name="Category" DefaultValue="" />  
    </WhereParameters>  
</asp:LinqDataSource>

The Where parameters are used with the dropdownlist filters on the header of the gridview. They can be null so that dsGridView will return all records.

My gridview has paging enabled.

Table Ticket has a field called TimeSpent. I would like to calculate the total TimeSpent for all the tickets filtered and display it on Footer.

I could use Linq2SQL on gridView_DataBound to query ALL tickets' TimeSpent. However, I am confused how to get the total TimeSpent when the gridview is filtered.

I tried to get tickets from LinqDataSourceSelectEventArgs.Result, but it only returned the total TimeSpent for the current page of gridview, not for the whole table.

The problem is, I do not know how many Where parameters will present in the Selecting event. Department can be null and not shown up in WhereParameters, and so can Category.

Something like this:

TicketsDataContext db = new TicketsDataContext();  
var query = from ticket in db.Tickets select ticket;  
foreach (var param in dsGridView.WhereParameters  
{  
    if (!string.IsNullOrEmpty(param.Value))  
        query.query.Where(...)  
}

Does not work of course. Is there any idea how I could tackle this issue? Thanks in advance!

Updated: I ended up reusing the data returned from dsGridView in OnSelected event as below:

protected void dsGridView_Selected(Object sender, LinqDataSourceStatusEventArgs e)  
{  
    var totalTime = (e.Result as List<Ticket>).Sum(t => t.TimeSpent);  
    grvTickets.Columns[7].FooterText = "Sum: " + totalTime.ToString();  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方。的韩爷 2024-09-22 23:52:21

您可以一点一点地构建查询。它确实有效,尽管写起来很乏味且丑陋。

TicketsDataContext db = new TicketsDataContext();
var query = from ticket in db.Tickets select ticket;

If (param.Name == "Department"){
    if (!string.IsNullOrEmpty(param.Value)){
        query = query.Where(c => c.Department == param.Value);
     }
}

If (param.Name == "Category"){
    if (!string.IsNullOrEmpty(param.Value)){
        query = query.Where(c => c.Category == param.Value);
    }
}

我不确定 param.Name 是否有效,我的 C# 可能略有错误,但希望这能让您开始。

You could build up the query bit by bit. It works, albeit tedious and ugly to write.

TicketsDataContext db = new TicketsDataContext();
var query = from ticket in db.Tickets select ticket;

If (param.Name == "Department"){
    if (!string.IsNullOrEmpty(param.Value)){
        query = query.Where(c => c.Department == param.Value);
     }
}

If (param.Name == "Category"){
    if (!string.IsNullOrEmpty(param.Value)){
        query = query.Where(c => c.Category == param.Value);
    }
}

I'm not sure if the param.Name is valid and my C# my be slightly wrong but hopefully that'll get you started.

南风起 2024-09-22 23:52:21

http://www.albahari.com/nutshell/predicatebuilder.aspx 开始。它们进入 PredicateBuilder,这对于您将遇到的大多数场景都有好处。最终您可能需要深入研究表达式树,但这要高级得多。

从您下面的评论来看,听起来 Dynamic Linq 更适合您:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

如果这还不够,您必须自己构建表达式树。别担心,你想做的事情绝对是可能的。如果您必须自己做的话,可能会有点棘手。

Start with http://www.albahari.com/nutshell/predicatebuilder.aspx. They go into PredicateBuilder which is good for most of the scenarios you'll run into. Ultimately you may need to delve into expression trees, but that's a good deal more advanced.

From your comment below, it sounds like Dynamic Linq will be a better fit for you:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

If that doesn't suffice, you'll have to build the expression trees yourself. Don't worry, what you want to do is definitely possible. Might get a bit tricky if you have to do it yourself though.

笑咖 2024-09-22 23:52:21

如果 where 子句中的参数数量有限,您可以执行以下操作:

TicketsDataContext db = new TicketsDataContext();
var query = from ticket in db.Tickets 
            where ticket.Department == (!string.IsNullOrEmpty(DepartmentParam.Value) ? DepartmentParam.Value : ticket.Department) &&
                  ticker.Category == (!string.IsNullOrEmpty(CategoryParam.Value) ? CategoryParam.Value : ticket.Category)
            select ticket;

这样,如果参数为空,则它只是将该字段与其自身进行比较。

If you have a limited number of parameters in your where clause you can do the following:

TicketsDataContext db = new TicketsDataContext();
var query = from ticket in db.Tickets 
            where ticket.Department == (!string.IsNullOrEmpty(DepartmentParam.Value) ? DepartmentParam.Value : ticket.Department) &&
                  ticker.Category == (!string.IsNullOrEmpty(CategoryParam.Value) ? CategoryParam.Value : ticket.Category)
            select ticket;

That way if the parameter is blank then it simply compares the field with itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文