使用 linq to sql 动态选择列(数量)
我有一个像这样的表,
Student Exam p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12
-----------------------------------------------------------------------------------
100 unit1 89 56 59 28 48 38 0 0 0 0 0 0
100 unit2 89 56 59 0 0 0 0 0 0 0 0 0
100 unit3 89 56 59 28 48 38 0 0 0 0 0 0
100 unit4 89 56 59 28 48 0 0 0 0 0 0 0
另一个表现
Exam Num_subjects
----------------------
unit1 6
unit2 3
unit3 6
unit4 5
在我需要选择单元 1 的标记表中唯一的前 8 列,因为单元 1 的主题数是 6 ..如何动态执行此操作... 考试是 linq to sql 中标记表的外键任何想法...
I have a table like this
Student Exam p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12
-----------------------------------------------------------------------------------
100 unit1 89 56 59 28 48 38 0 0 0 0 0 0
100 unit2 89 56 59 0 0 0 0 0 0 0 0 0
100 unit3 89 56 59 28 48 38 0 0 0 0 0 0
100 unit4 89 56 59 28 48 0 0 0 0 0 0 0
another table
Exam Num_subjects
----------------------
unit1 6
unit2 3
unit3 6
unit4 5
now i need to select the only first 8 columns in the marks table for unit1 as the number of subject for the unit1 is 6 .. how to do this dynamically ...
exam is foreign key to the marks table in linq to sql any ideas ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有基于列的设计,由于 L2S 不允许您手动具体化(即 new MyTable { Foo = row.Foo /* omit some } ,您会有点失望。
如果您 只是想要数据,你可以使用像“dapper”这样的东西,它不会有这个问题,但是你需要自己编写TSQL,即
但最终,我想我更喜欢不同的数据库模式这里...
If you have a column based design, since L2S doesn't let you manually materialize (i.e.
new MyTable { Foo = row.Foo /* omit some }
you are a bit scuppered.If you just want the data, you could use something like "dapper" which won't have this issue, but you'll need to write the TSQL yourself, i.e.
But ultimately, I think I'd prefer a different db schema here...
如果这些表被规范化,就不需要动态查询。 (检查您的设计)
如果您确实想要动态地执行此操作,则需要一个表达式树来处理查询的选择部分...... asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" rel="nofollow">在这里你可以找到有关 a 的更多详细信息动态查询库可以为您处理表达式树生成(您可以提供像“new(p1,p2,p3)”这样的字符串,并将其转换为表达式树)
there would be no need for a dynamic query, if those tables were normalized. (check your design)
if you really want to do this dynamically, you'll need an expression tree that handles the select part of your query ... here you can find some more details about a dynamic query lib that can handle that expression tree generation for you (you can provide a string like "new(p1,p2,p3)" and that gets translated to an expression tree)
这里你不需要使用linq,你可以用逻辑来完成
现在只需获取Num_Subjects就像unit1 = 6
最后你我们得到一个表最多P6列其余的列将被删除。
Here you don't need to use linq, you can do it with logic
Now just get Num_Subjects like for unit1 = 6
At last you we get a table upto P6 columns rest of columns will be deleted.