Linq 子查询(SELECT 中的 SELECT)
我在 MySQL 实体中遇到了本机连接子查询问题。 SQL 代码是;
数据库结构是;
tbl_urunler_kod
id、kod
- 1、pkod1
- 2、pkod2
- 3、pkod3
tbl_urunler
id、kod_id
- 1, 1
- 2, 2
- 3, 3
- 4,空
- 5,空
- 6,空
- 7,空
- 8,空
- 9, null
SELECT (SELECT k.kod FROM tbl_urunler_kod k WHERE k.id=t.kod_id) AS kod FROM tbl_urunler t
给出 9 条记录。
我尝试了一下,
var SQL = (from p in SME.tbl_urunler
from k in SME.tbl_urunler_kod
where k.id == p.kod_id
select new
{
kod = k.kod
});
它只返回 3 条记录。因为 tbl_urunler_kod 只得到了 not null 3 条记录。其他6条记录为空。我想查看所有记录。我怎样才能做到这一点。
我尝试这个;
var SQL2 = (from p in SME.tbl_urunler
select new
{
kod = (from k in SME.tbl_urunler_kod where k.id == p.kod_id select new { k.kod })
});
它给出了这个错误;
'System.Data.Common.Internal.Materialization.CompensatingCollection`1[f__AnonymousType0`1[System.String]]' türündeki nesne 'System.String' türüne atılamadı. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: 'System.Data.Common.Internal.Materialization.CompensatingCollection`1[f__AnonymousType0`1[System.String]]' türündeki nesne 'System.String' türüne atılamadı.
感谢您的帮助。
I have got a native join sub query problem in MySQL Entity. SQL code is;
DB structure is;
tbl_urunler_kod
id, kod
- 1, pkod1
- 2, pkod2
- 3, pkod3
tbl_urunler
id, kod_id
- 1, 1
- 2, 2
- 3, 3
- 4, null
- 5, null
- 6, null
- 7, null
- 8, null
- 9, null
SELECT (SELECT k.kod FROM tbl_urunler_kod k WHERE k.id=t.kod_id) AS kod FROM tbl_urunler t
its giving 9 records.
I try this,
var SQL = (from p in SME.tbl_urunler
from k in SME.tbl_urunler_kod
where k.id == p.kod_id
select new
{
kod = k.kod
});
its returning only 3 records. Because tbl_urunler_kod have got only not null 3 records. Other 6 records is null. I wanna see all records. How can i do this.
And i try this;
var SQL2 = (from p in SME.tbl_urunler
select new
{
kod = (from k in SME.tbl_urunler_kod where k.id == p.kod_id select new { k.kod })
});
Its giving this error;
'System.Data.Common.Internal.Materialization.CompensatingCollection`1[f__AnonymousType0`1[System.String]]' türündeki nesne 'System.String' türüne atılamadı. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: 'System.Data.Common.Internal.Materialization.CompensatingCollection`1[f__AnonymousType0`1[System.String]]' türündeki nesne 'System.String' türüne atılamadı.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为此创建了一个小模拟。
请看结果;
结果 1
结果 2
结果 3
结果 4
结果 5
结果 6
结果 7
谢谢。
I was created a little simulatin for this.
Please look the results;
Result 1
Result 2
Result 3
Result 4
Result 5
Result 6
Result 7
Thanks.
您是否尝试过:
更新这是我从您的评论中可以理解的内容:
Have you tried:
Update here's what I could understand from your comments:
这是 LingtoSql lambda 表达式
this is LingtoSql lambda expression