如何使用linq在哪里+动态加入何处?

编程入门 行业动态 更新时间:2024-10-25 02:24:45
本文介绍了如何使用linq在哪里+动态加入何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好

字符串 userCond = 字符串 .Empty; 如果(!string.IsNullOrEmpty(data.FirstName)) { userCond = " 名字== \"" + data.FirstName + " \; var query = from ctx.User_Users.Where(userCond)中的x 选择x; }

当我执行以上代码时,它会产生以下错误: 在当前作用域或上下文中无法解析名字".请确保所有引用的变量都在作用域内,已加载所需的架构,并且正确引用了名称空间."

解决方案

有什么理由不能使用lambda吗?对于您发布的代码,无论如何您似乎都知道您正在使用名字.

var query = ctx.User_Users.Where(u => u.FirstName == data.FirstName);

干杯.另一个具有col1(从F1到table1的col)和col4的表table2,最终用户仅选择table1的参数col,则where子句中应该只有一个列,即col1并加入,但如果用户要求基于table1的col1进行搜索和table2的col3,则它需要where子句中的两列以及join. 希望它能清除需求.

Hi Everyone

string userCond = string.Empty; if (!string.IsNullOrEmpty(data.FirstName)) { userCond = "FirstName ==\"" + data.FirstName + "\"; var query = from x in ctx.User_Users.Where(userCond) select x; }

When i execute above code it generates error mentioned below: "FirstName'' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly." Is there any work around to this problem?

解决方案

Is there any reason you can''t use the lambda? For the code you''ve posted it seems as though you know you''re working with the first name, anyways.

var query = ctx.User_Users.Where(u => u.FirstName == data.FirstName);

Cheers.

Basically what i want is to apply join and where clause dynamically based upon type of parameter selected by end user through user interface e.g if i have a table table1 with columns col1 and col2 and another table table2 with col1 (FK to col of table1) col3 and col4 and end user selects only parameter col of table1 then there should be only one column i.e col1 in the where clause and join but if user asks to search based on col1 of table1 and col3 of table2 then it requires both columns in the where clause as well as join. Hope it clears the need.

更多推荐

如何使用linq在哪里+动态加入何处?

本文发布于:2023-11-16 21:58:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   动态   linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!