自动完成扩展器“扩展”跨整个数据表

发布于 2024-09-06 13:48:23 字数 700 浏览 11 评论 0原文

我请求创建一个自动完成功能来搜索数据表。这是否可以快速简单地实现,或者是否需要编写合理数量的代码?

最初,我一直在使用 Web 服务和 linq 来指向单个列的数据值 (IDDesc) 并拉回产品列表:

Product.FinalProductsDataContext dbac = new Product.FinalProductsDataContext();

            return dbac.tblProduct
                .Where(r => r.Account== HttpContext.Current.Session["AccountKey"].ToString() && r.IDDesc.Contains(prefixText))
                .Distinct()
                .OrderBy(r => r.IDDesc)
                .Select(r => r.IDDesc)
                .Take(count)
                .ToArray();

但是,如果我希望自动完成功能查看所有列,是否会重复对于数据表中包含的每个列是否有类似的 LINQ 语句,或者是否有“快速修复”?

我个人认为这不是一个理想的情况,但这是我必须努力实现的要求。

任何帮助或建议,不胜感激。

I have a request to create an auto complete that will search an data table. Is this achieveable quickly and simply or is it a case of writing a reasonable amount of code?

Originally, I have been using a webservice and linq to point at a single column's worth of data (IDDesc) and pull back the list of products:

Product.FinalProductsDataContext dbac = new Product.FinalProductsDataContext();

            return dbac.tblProduct
                .Where(r => r.Account== HttpContext.Current.Session["AccountKey"].ToString() && r.IDDesc.Contains(prefixText))
                .Distinct()
                .OrderBy(r => r.IDDesc)
                .Select(r => r.IDDesc)
                .Take(count)
                .ToArray();

However, if I wish the autocomplete to look at all the columns, is it a case of repeating similar LINQ statements for each of the columns contained within the datatable or is there a 'quick fix'?

I personally don't think this is an ideal scenario but it is a request I must work towards.

Any help or advice, greatly appreciated.

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

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

发布评论

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

评论(1

十秒萌定你 2024-09-13 13:48:23

我认为我应该在数据库中放入一些内容,而不是尝试使用 LINQ 来完全解决这个问题(并对表中的每一列重复所有这些语句,以及重复访问数据库)在这里做繁重的工作。

您可以创建一个视图,该视图从表中获取字段并将它们合并到一列中,例如,

CREATE VIEW [dbo].[ProductView]
AS
SELECT     CAST(ProductName AS NVARCHAR(50)) AS 'ProductColumn'
FROM         dbo.Products
UNION
SELECT CAST(SupplierName AS NVARCHAR(50))
FROM dbo.Products
UNION
...

如果您将视图添加到上下文中,则将允许您修改现有的 LINQ 查询并将其指向该视图,例如:

Product.FinalProductsDataContext dbac = new Product.FinalProductsDataContext();

return dbac.ProductView
    .Where(r => r.Account== HttpContext.Current.Session["AccountKey"].ToString() && r.ProductColumn.Contains(prefixText))
    .Distinct()
    .OrderBy(r => r.ProductColumn)
    .Select(r => r.ProductColumn)
    .Take(count)
    .ToArray();

Rather than trying to solve this entirely with LINQ (and repeating all those statements for each column in the table, as well as hitting the database repeatedly), I think I'd look to put something in the database to do the heavy lifting here.

You could create a view that takes the fields from the table and amalgamates them into one column e.g.

CREATE VIEW [dbo].[ProductView]
AS
SELECT     CAST(ProductName AS NVARCHAR(50)) AS 'ProductColumn'
FROM         dbo.Products
UNION
SELECT CAST(SupplierName AS NVARCHAR(50))
FROM dbo.Products
UNION
...

which if you added the view to your context would then allow you to modify your existing LINQ query and point it at that view e.g.:

Product.FinalProductsDataContext dbac = new Product.FinalProductsDataContext();

return dbac.ProductView
    .Where(r => r.Account== HttpContext.Current.Session["AccountKey"].ToString() && r.ProductColumn.Contains(prefixText))
    .Distinct()
    .OrderBy(r => r.ProductColumn)
    .Select(r => r.ProductColumn)
    .Take(count)
    .ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文