如何使用WPF和Linq-to-entities模型搜索数据库 [英] How can I search into database with WPF and Linq-to-entities model

查看:71
本文介绍了如何使用WPF和Linq-to-entities模型搜索数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我准备一个WPF项目,在这里我想实施一个更复杂的搜索.我通过ADO.NET实体模型使用LINQ来创建实体,并计划在WPFToolkit DataGrid中进行显示.

I prepare a WPF project, where I want to implement a more complex search. I use LINQ to entities through the ADO.NET Entity model and plan to do the display in WPFToolkit DataGrid.

我的搜索窗口应允许通过一些不同的条件进行搜索.我的想法是能够在应用程序中输入(例如)名称,姓氏和职业文本框,并接收所选表中与所有3个搜索参数相对应的所有人员的列表.我希望能够在输入全部或仅输入一个时进行搜索.

My search window should allow search by a few different criteria. My idea is to be able to write in (for example) name, surname and occupation textboxes in the application and receive a list of all people in the selected table that correspond to all 3 search parameters. I want to be able to search when all are entered or even if only one is entered.

我想另一个选择是将表单绑定到数据库...仍然,我不知道如何搜索(我知道如何显示信息,编辑信息并插入...但不搜索)

I suppose that the other option is to bind the form to the database...still, I do not know how to search like this (I know how to display info, edit it and insert...but not search).

我虽然将字符串应用于查询机制,但徒劳无功.似乎无法正常工作.请分享一些有关如何执行此操作的想法.任何源代码或代码片段都将受到赞赏.

I though of applying a string to query mechanism, but in vain. Seems that it's not working. Please, share some ideas of how can I do this. Any source or code fragment will be appreciated.

谢谢!

推荐答案

Linq使得组合查询非常容易,因此您可以从更简单的构建基块中构建查询.对于这样的系统,您还可以选择多种级别.您可以拥有一个查询的一般形式为静态的系统,这是标准的Linq查询,但是您可以一直使用自定义查询描述格式,然后将其转换为Linq语句.

Linq makes it really easy to compose queries so that you can build them up from simpler building blocks. There are also a wide range of levels you can go to for a system like this. You can have a system where the general form of the query is static, which is your standard Linq query, but you can go all the way to having a custom query description format that you then convert into a Linq statement.

在您的情况下,您似乎只有几个可能的过滤器.您可以这样做:

It looks like in your case you have just a few possible filters. You can do it similar to this:

var Query = Context.MyDataSet; //Whatever is the standard base query

if (!string.IsNullOrEmpty(NameFilter))
    Query = Query.Where(e => e.Name.Contains(NameFilter));

if (!string.IsNullOrEmpty(SurnameFilter))
    Query = Query.Where(e => e.Surname.Contains(SurnameFilter));

...

var Result = Query.ToList(); 

只要您有一组固定的参数可供用户搜索,就可以通过选择添加过滤器来使用这种构建查询的方法.

As long as you have a fixed set of parameters users can search on, you can go pretty far with this method of building queries by optionally adding filters.

这篇关于如何使用WPF和Linq-to-entities模型搜索数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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