Tag: linq to entities

使用SQL LIKE运算符的LINQ to Entities

我有这个: query = query.Where(s => s.ShowTypeDescription == showTypeDescription); 为了构建动态SQL,为了不同的变量多次。 我将如何改变以上内容来说: query = query.Where(s => s.ShowTypeDescription **LIKE** showTypeDescription); ?

为什么LINQ to Entities不识别某些方法?

为什么我不能这样做: usuariosEntities usersDB = new usuariosEntities(); foreach (DataGridViewRow user in dgvUsuarios.Rows) { var rowtoupdate = usersDB.usuarios.Where( u => u.codigo_usuario == Convert.ToInt32(user.Cells[0].Value) ).First(); rowtoupdate.password = user.Cells[3].Value.ToString(); } usersDB.SaveChanges(); 并且必须这样做: usuariosEntities usersDB = new usuariosEntities(); foreach (DataGridViewRow user in dgvUsuarios.Rows) { int usercode = Convert.ToInt32(user.Cells[0].Value); var rowtoupdate = usersDB.usuarios.Where(u => u.codigo_usuario == usercode).First(); rowtoupdate.password = user.Cells[3].Value.ToString(); } […]

C#Linq字符串与indexOf进行比较

我有一个表,其中包含一个包含主机名的字符串字段。 它们大多是完全合格的域名,但多年来第一个“点”之后的域位已经改变,因为各种DNS变化已经落在我们身上。 所以我可能将表中的机器“tom”作为: tom.company.com tom.it.company.com tom.newComapnyBranding.com … 我经常要对“当前”主机名和这个历史商店进行比较。 做类似的事情: WHERE UPPER(SUBSTRING(@foo, 1, CHARINDEX(“.”, @foo))) = UPPER(SUBSTRING(myDB.myTable.machineName, 1, CHARINDEX(“.”, myDB.myTable.machineName))) 好吧,我试图将其中一个转换为Linq查询,但我在“索引”部分磕磕绊绊。 我差一点接近: myTable.machineName.ToUpper().Substring(0, myTable.machineName.IndexOf(“.”)) .Equals(foo.ToUpper().Substring(0, foo.IndexOf(“.”))) 但是visual studio正在抱怨“IndexOf”。 它声称我需要将IndexOf更改为: IndexOf(“.”, StringComparison.Ordinal)) 但是当我运行它时,我收到exception消息: LINQ to Entities does not recognize the method ‘Int32 IndexOf(System.String, System.StringComparison)’ method, and this method cannot be translated into a store expression 你如何在Linq中使用这种基于索引的子字符串?

LINQ to Entities无法识别方法’System.String get_Item(System.String)’,

我怎么解决这个问题? 这是我的代码: DateTime dtInicio = new DateTime(); DateTime dtFim = new DateTime(); Int32 codStatus = 0; if(!string.IsNullOrEmpty(collection[“txtDtInicial”])) dtInicio = Convert.ToDateTime(collection[“txtDtInicial”]); if(!string.IsNullOrEmpty(collection[“txtDtFinal”])) dtFim = Convert.ToDateTime(collection[“txtDtFinal”]); if (!string.IsNullOrEmpty(collection[“StatusCliente”])) Convert.ToInt32(collection[“StatusCliente”]); var listCLientResult = (from c in db.tbClientes orderby c.id where (c.effdt >= dtInicio || string.IsNullOrEmpty(collection[“txtDtInicial”]) && (c.effdt <= dtFim || string.IsNullOrEmpty(collection["txtDtFinal"])) && (c.cod_status_viagem == codStatus || string.IsNullOrEmpty(collection["StatusCliente"]))) select […]

排序自引用关系

假设以下模型。 请注意自引用关系“parent”。 public class Category { public virtual long Id { get; set; } public virtual string Name { get; set; } public virtual Category Parent { get; set; } public virtual long? ParentId { get; set; } } 我的数据如下: id | name | parentId 1——–tag 1 —– null 2——–tag 2 —– 1 3——–tag 3 […]

如何在Linq中将Arithabort设置为实体?

我已经找到了很多关于如何在Linq中将Arithabort设置为SQL的答案,但在Linq to Entities中没有任何答案。 在Linq to SQL中,您可以这样做: using (var conn = new SqlConnection(connectionString)){ cmd = conn.CreateCommand(); cmd.Connection.Open(); cmd.CommandText = “set arithabort on;”; cmd.ExecuteNonQuery(); // Line 5 using (var db = new MyDataContext(conn)) { … } } 但是如果我做同样的事情,只是在上面的代码片段中用SqlConnection替换EntityConnection ,我在第5行得到一个运行时错误: 查询语法无效。 标识符’arithabort’附近,第1行,第5列。 我猜它与Linq2Sql硬连线的事实有关,而EF可以在其他数据库上工作。 那么诀窍是什么?

我们可以使用Linq将匿名类型转换为List 到C#中的实体吗?

我有一个包含三列的表我想使用自联接并检索具有别名的列。 表: Material(ID,Name,MaterialParentID) public List GetMaterialList() { List materilaList = new List(); var query = (from c1 in db.Materials join c2 in db.Materials on c1.ID equals c2.MaterialParentID select c2); return query.ToList(); } 我想在现有查询中添加以下内容并返回List select new { c2.ID, c2.MaterialParentID, c2.Name, ParentName = c1.Name })

LINQ查询列表中的列表

我有这种情况: 我的ModelView: public class Subject { public int ID { get; set; } public string Name { get; set; } public int ProfessorID { get; set; } public string ProfessorFullName{ get; set; } public IList Assistants { get; set; } } public class Assistant { public string AssistantFullName{ get; set; } } 我的查询: var subjects […]

将SQL子查询转换为In到Linq Lambda

如何将以下SQL语句转换为Lambda表达式或Linq查询? 以下查询获取每个问题的单个最新答案。 或者用另一种方式来表达,用最新的答案得到每个问题。 这也将由Entity Framework执行。 SELECT Answers.* FROM Answers Where AnswerID IN ( SELECT Max(AnswerID) AnswerID FROM Answers GROUP BY QuestionID ) 这是使用内部联接查看上一个查询的另一种方法 SELECT answers.* FROM answers INNER JOIN ( SELECT Max(answerID) answerID –, QuestionSiteID FROM answers GROUP BY QuestionID ) t ON answers.answerID = t.answerID 我已经读过LINQ Contains方法对于访问SQL的查询是次优的。 LINQ to Sql和.Contains()陷阱。

使用Dynamic Expression API选择匿名类型

我正在使用带有LINQ to Entities的动态表达式API( System.Linq.Dynamic )。 我的LINQ查询如下。 var query = this.db.Products.AsQueryable() .Where(strCondition) .OrderBy(“ProductNumber”) .Select(“new(ProductNumber, ProductDescription, ProductCategory.Name)”); 现在我有了“查询”,我不知道如何获得每个字段的值。 string strTemp; foreach (var item in query) { strTemp = item.? } 它是匿名类型,所以我不能真正使用强类型来获取值。 我能做什么? 我选择获取匿名类型字段的原因是因为我需要将ProductCategory.Name字段放入结果中。 有没有更好的方法在Dynamic Expression API的结果中获取ProductCategory.Name? 有人可以帮忙吗?