我需要从某个地方获得一些灵感,并希望有人,任何人都可以提供帮助。
这源于我创建的前一个帖子,我希望能够在完整的数据表上进行自动完成搜索,即所有列中的所有数据。
我创建了一个存储过程,它将自动完成项和这些项的派生列合在一起,即
tblAutoCompleteItems:
Item Column
Item1 Product
Item2 Product
Item3 Product
Red Category1
Green Category1
Blue Category1
Small Category2
Medium Category2
Large Category2
我已经对文本框自动完成进行了排序,并且工作正常。根据客户端选择,标签显示生成此项目的相关列(因此客户可以检查他们对搜索将返回的内容感到满意)。可爱。
然而,棘手的位和我需要帮助的位是根据他们选择的标准拉回另一个数据集。其他数据集看起来像......
tblProductInfo:
Product Category1 Category2
Item1 Red Big
Item2 Red Small
Item3 Blue Small
例如,用户自动填充'Item1',文本框显示此内容,标签显示'Product'或者,用户可能希望按Category1搜索,因此搜索'Red',这将显示在文本框中,标签显示'Category1'
在查看tblProductInfo时,我需要以某种方式查询数据集,以便它查看自动完成列和自动完成项。
所以,当我点击按钮或其他任何东西时,linq首先查看该列,说“哦,我知道,我需要查看Category1然后将其中的任何内容拉回来”或“确定这是一个产品,我首先转到Product列,然后撤回与Item1“
相关的数据我在两个方面苦苦挣扎(1)如何访问表中的列名,以及(2)在linq中构造where子句。
感激不尽的任何帮助,提示或建议 - 即使这意味着完整的策略重新思考。
我正在使用c#,LINQ和asp.net。
通过 - 我意识到在完整的桌子上看到自动完成效果并不理想,但这不在我的手中......很遗憾。
答案 0 :(得分:0)
有几种方法可以获取元数据;您可以通过存储过程从数据库中提取它(SQL,Oracle具有可以提取表的元数据的视图),或者如果您使用的是LINQ to SQL,则可以使用Reflection来反映代表tblProductInfo的类。
要构造where子句,您需要使用动态LINQ:http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
因为这允许您使用字符串列名来执行查询。
HTH。
答案 1 :(得分:0)
令人难以置信的是,我自己也搞定了!如果有人偶然发现并寻找类似的东西,这里有一些可爱的伪代码:
public static List<tblInfo> GetProductInfo(string memberid, string locationid, string column, string acitem)
{
MyEntities getproductinfo = new MyEntities ();
var r = (from p in getproductinfo.tblProductInfo
where p.MemberId == memberid &&
p.LocationId == locationid
select p);
if (column == "Product")
r = r.Where(p => p.Product == acitem);
if (column == "Category1")
r = r.Where(p => p.Category1 == acitem);
return r.OrderBy(p => p.Product).ThenBy(p => p.Category1).ToList();
答案 2 :(得分:0)
我没有通过编译器运行它,但我认为语法很好。应该导致IEnumerable
var r = (from p in getproductinfo.tblProductInfo
where p.MemberId == memberid
&& p.LocationId == locationid
&& column == "Product" ? p.Product == acitem :
column == "Category1" ? p.Category1 == acitem : true
orderBy p.Product
thenBy p.Category1
select p);
答案 3 :(得分:0)
对你的一点修改:
MyEntities getproductinfo = new MyEntities ();
var r = (from p in getproductinfo.tblProductInfo
where p.MemberId == memberid &&
p.LocationId == locationid
order by p.Product, p.Category1
select p);
if (column == "Product")
r = r.Where(p => p.Product == acitem);
if (column == "Category1")
r = r.Where(p => p.Category1 == acitem);
return r.ToList();
LINQ会将Where放在适当的位置。