简体   繁体   English

实体框架选择查询

[英]Entity Framework selection query

I am making a simple application for insert, update, delete, select data with Entity Framework 我正在使用Entity Framework制作一个简单的插入,更新,删除,选择数据的应用程序
I already made insertion, deletion and select all data. 我已经插入,删除并选择了所有数据。

Now I want to select with where condition with filter of two fields 现在我想选择具有两个字段的过滤器的where条件
For ex : I have table with 例如:我有桌子

userid
username
password
email

Now need selection like where email = "" and password = "" 现在需要选择例如where email = "" and password = ""

I know how to write query in SQL but having not clues with entity framework . 我知道如何在SQL中编写查询但没有entity framework线索。
Also need to store this result in datatable and looping solution both for learning purpose. 还需要将此结果存储在数据表和循环解决方案中,以用于学习目的。
This can help many beginners 这可以帮助许多初学者

Using Linq To Entities with lambda expression: 使用具有lambda表达式的Linq To Entities:

var result = dBContext.Account.Where(a=> a.email == "" && a.password =="").ToList();

Using Linq To Entities the less fancy way: 使用Linq To Entities的方式不那么花哨:

var result = (from a in dBContext.Account
              where a.email == "" && a.password ==""
              select a).ToList();

Lambda expressions are used most of the time. Lambda表达式大多数时候都在使用。 Some people find lambda's less readable. 有些人发现lambda的可读性较差。 I think it's more a personal taste that depends from your background. 我认为这更多的是个人品味,取决于你的背景。

EDIT: 编辑:

dbContext should be replaced by the name you gave your dbContext/Entities when setting up your Entitiy framework EDMX or Code First Classes. 在设置Entitiy框架EDMX或代码优先类时,dbContext应替换为您为dbContext / Entities提供的名称。

Account should be replaced by the name of your table/Entity 帐户应替换为您的表/实体的名称

To loop and edit the results you can do: 要循环和编辑结果,您可以执行以下操作:

foreach(var account in results)
{
   //do something with the properties
   account.email = "test@updated.com"
}

//store the changes again in the db
dbContext.SaveChanges();

Use linq ex: 使用linq ex:

List<User> result = dbContext.Users.Where(o=> o.email == "" && o.password=="").ToList();

foreach(User u in result)
{
// do stuff
}

User u = new User();
u.Email = "mail@mail.com";

dbContext.Users.Add(u);
dbContext.Save(); //I think its Save()..

Have to use Linq query like 必须使用Linq查询之类的

var data= dBContext.Account.Where(a=> a.email == "" && a.password =="").ToList();

.ToList() will give your entire data that comes under your where condition or filter. .ToList()将提供您的where条件或过滤器下的整个数据。 Now for that you return DataTable and easily apply a for condtion based on your DataTable values. 现在为您返回DataTable ,轻松地应用for基于您condtion DataTable值。

DataTable tempData = (DataTable)grdRecords.DataSource;

var query = from r in tempData.AsEnumerable()
            where r.Field<string>("UserName") != "TestUsername" &&
                  r.Field<string>("Password") != "TestPassword"
            select r;

DataTable newDT = query.CopyToDataTable();

use linq query as follows 使用linq查询如下

IList<Users> Users = dbContext.Users.Where(x=> x.email == "" && x.password=="").ToList();

then if you want to convert into DataTable just call following generic method do convert it 那么如果你想转换成DataTable,只需调用以下泛型方法来转换它

        public DataTable ToDataTable<T>(IList<T> data)// T is any generic type
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));

            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

You can try this 你可以试试这个

databasenameEntities db = new databasenameEntities ();

tablename exist= db.tablename.where(p=>p.email=="txtemail.text" && p.password=="txtpassword.text");

if(exist.count>0)

{

txtuserid.text=Convert.toInt32(exist.userid);

txtusername.Text=exist.username;

....

.....

db.saveChanges();

}

hope this may helpful. 希望这可能有所帮助。

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

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