简体   繁体   English

在C#中使用Entity Framework搜索数据库

[英]Searching database using Entity Framework in C#

I have a piece of code below which works fine but I want something else to get as the result. 我下面有一段代码可以正常工作,但是我希望得到其他结果。 This code shows the first row from the database table that matches the searching criteria. 此代码显示数据库表中符合搜索条件的第一行。

userName tbl = new userName();

bool flag = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).Any();

if (flag)
{
    tbl = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).First();
    userNameBindingSource.DataSource = tbl;
}
else
{
    MessageBox.Show("This record does not exist in the database.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

But what if I have multiple rows that match the searching criteria. 但是,如果我有符合搜索条件的多行怎么办。 I want to show all of them in the result list, not just the first one which matches the criteria. 我想在结果列表中显示所有它们,而不仅仅是与条件匹配的第一个。

I have tried this, but something went wrong: 我已经尝试过了,但是出了点问题:

tbl = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).All();

How should I refine the code above to achieve this? 我应该如何完善上面的代码来实现这一目标?

If you want to get back a List of items you can use LINQ .ToList() on end of your query. 如果要返回项目列表,则可以在查询末尾使用LINQ .ToList()。

List<UserName> tbl = db.UserNames.Where(x => x.Name == txtName.Text || x.City == txtCity.Text).ToList();

This will evaluate your IQueryable that you build with DBContext. 这将评估您使用DBContext构建的IQueryable。 ToList will return a list of your entities. ToList将返回您的实体的列表。 You can use ToArray also :) 您也可以使用ToArray :)

.All() is for validating all your item in your enumeration match with your expression. .All()用于验证枚举中与表达式匹配的所有项目。 Example: 例:

.All(x=> x.Contains("text")).

Change this: 更改此:

tbl = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).First();

to this: 对此:

var items = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).ToList();

Using the 'var' just means the system will evaluate the best type by implying usage. 使用“ var”只是意味着系统将通过暗示用法来评估最佳类型。 Unless you want to call out a specific, which most times is no longer needed. 除非您想指定一个特定的,否则不再需要大多数时间。

All of this: 所有这些:

bool flag = db.userName.Where(x => x.name == txtName.Text || x.City==txtCity.Text).Any();

if (flag)
{
 // code...
}

is completely unneeded as most things that are nice about LINQ is you can do most of the predicate usage directly inline with .Where, .Any, .Exists, .Contains, etc... Then you merely chain off of that with more extension methods something like: 完全不需要LINQ的大多数优点是,您可以直接与.Where,.Any,.Exists,.Contains等直接内联使用大多数谓词,然后只需使用更多扩展方法将其链接在一起即可。就像是:

context.(tableOrObject)
  .Where(x => x.Prop == (somePredicate))
  .Select(x => new { Id = x.Id, Desc = (some series of things)})
  .ToList();

When you do something like.First, .Single, .FirstOrDefault, etc... you are limiting your collection to a single return. 当您执行诸如.First,.Single,.FirstOrDefault等之类的操作时,您会将集合限制为单返回。 .ToList() or .ToArray() are more common for realizing your data into a collection return. .ToList()或.ToArray()在将数据实现为集合返回中更为常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM