Linq子查询(SELECT中的SELECT)

时间:2011-12-15 01:19:08

标签: linq entity-framework entity subquery

我在MySQL实体中遇到了一个本机连接子查询问题。 SQL代码是;

DB结构是;

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

它给出了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只有空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ı.

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

你试过了吗?

var SQL = (from p in SME.tbl_urunler
               from k in SME.tbl_urunler_kod
                          where k.id == p.kod_id || p.kod_id == null
               select new
               {
                   kod = k.kod
               });

更新这是我从您的评论中可以理解的内容:

var q = from u in SME.tbl_urunler
    join k in SME.tbl_urunler_kod on u.kod_id equals k.id into JoinedUK
    from k in joinedUK.DefaultIfEmpty()
    select new
    {
        id = u.id,  
        kod = k != null ? k.kod : null
    }

答案 1 :(得分:1)

这是LingtoSql lambda表达式

    var query = SME.tbl_urunler.Where
                (x=>x.tbl_urunler_kod.id == x.kod_id  || 
                x.kod_id == null).Select(x=>new {x.id})

答案 2 :(得分:1)

我为此创造了一点simulatin。 请查看结果;

public class tbl_urunler_kod
{
    public int ID { get; set; }
    public string Kod { get; set; }
    public tbl_urunler_kod() { }
    public tbl_urunler_kod(int _ID, string _Kod)
    {
        _ID = ID;
        _Kod = Kod;
    }
}

public class tbl_urunler
{
    public int ID { get; set; }
    public string Adi { get; set; }
    public int KodID { get; set; }
    public tbl_urunler() { }
    public tbl_urunler(int _ID, string _Adi, int _KodID)
    {
        _ID = ID;
        _Adi = Adi;
        _KodID = KodID;
    }
}

public List<tbl_urunler_kod> L_tbl_urunler_kod = new List<tbl_urunler_kod>();
public List<tbl_urunler> L_tbl_urunler = new List<tbl_urunler>();

protected void Page_Load(object sender, EventArgs e)
{
    Fill();
    Result1();
    Result2();
    Result3();
    Result4();
    Result5(); //Its working
    Result6(); //Its working
    Result7(); //Its working
}

public void Fill()
{
    #region tbl_urunler_kod filling
    L_tbl_urunler_kod.Add(new tbl_urunler_kod { ID = 1, Kod = "Kod_1" });
    L_tbl_urunler_kod.Add(new tbl_urunler_kod() { ID = 2, Kod = "Kod_2" });
    L_tbl_urunler_kod.Add(new tbl_urunler_kod() { ID = 3, Kod = "Kod_3" });
    #endregion

    #region tbl_urunler filling
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_1", ID = 1, KodID = 1 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_2", ID = 2, KodID = 2 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_3", ID = 3, KodID = 3 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_4", ID = 4 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_5", ID = 5 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_6", ID = 6 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_7", ID = 7 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_8", ID = 8 });
    L_tbl_urunler.Add(new tbl_urunler() { Adi = "Urun_9", ID = 9 });
    #endregion
}

public void Result1()
{
    Response.Write("<h1>Result 1</h1>");

    var SQL = (from p in L_tbl_urunler
               from k in L_tbl_urunler_kod
               where k.ID == p.KodID
               select new
               {
                   p.Adi,
                   kod = k.Kod
               }); //result count = 3 record;

    foreach (var i in SQL)
    {
        Response.Write(i.Adi + " - " + i.kod + "<br/>");
    }

    Response.Write("<br/><hr/>");
}

public void Result2()
{
    Response.Write("<h1>Result 2</h1>");

    var SQL = (from p in L_tbl_urunler
               from k in L_tbl_urunler_kod
               where k.ID == p.KodID || p.KodID == 0
               select new
               {
                   p.Adi,
                   kod = k.Kod
               }); //result count = 21 record;

    foreach (var i in SQL)
    {
        Response.Write(i.Adi + " - " + i.kod + "<br/>");
    }

    Response.Write("<br/><hr/>");
}

public void Result3()
{
    Response.Write("<h1>Result 3</h1>");

    var SQL = (from p in L_tbl_urunler
               let LKod = L_tbl_urunler_kod.Where(k => k.ID == p.KodID).Select(x => x.Kod)
               select new
               {
                   p.Adi,
                   kod = LKod
               }); //result count = 9 record;

    foreach (var i in SQL)
    {
        Response.Write(i.Adi + " - " + i.kod + "<br/>");
        //System.Linq.Enumerable+WhereSelectListIterator`2[test+tbl_urunler_kod,System.String]
    }

    Response.Write("<br/><hr/>");
}

public void Result4()
{
    Response.Write("<h1>Result 4</h1>");

    var SQL = (from p in L_tbl_urunler
               select new
               {
                   p.Adi,
                   kod = (from k in L_tbl_urunler_kod where k.ID == p.KodID select new { k.Kod })
               }); //result count = 9 record;

    foreach (var i in SQL)
    {
        Response.Write(i.Adi + " - " + i.kod + "<br/>");
        //System.Linq.Enumerable+WhereSelectListIterator`2[test+tbl_urunler_kod,<>f__AnonymousType3`1[System.String]]
    }

    Response.Write("<br/><hr/>");
}

public void Result5()
{
    Response.Write("<h1>Result 5</h1>");

    var SQL = (from p in L_tbl_urunler
               select new
               {
                   p.Adi,
                   kod = L_tbl_urunler_kod.Where(k => k.ID == p.KodID).Select(x => x.Kod).FirstOrDefault()
               }); //result count = 9 record;

    foreach (var i in SQL)
    {
        Response.Write(i.Adi + " - " + i.kod + "<br/>");
    }

    Response.Write("<br/><hr/>");
}

public void Result6()
{
    Response.Write("<h1>Result 6</h1>");

    var SQL = (from p in L_tbl_urunler
               select new
               {
                   p.Adi,
                   kod = (from k in L_tbl_urunler_kod where k.ID == p.KodID select new { k.Kod }).FirstOrDefault()
               }); //result count = 9 record;

    foreach (var i in SQL)
    {
        Response.Write(i.Adi + " - " + i.kod + "<br/>");
    }

    Response.Write("<br/><hr/>");
}

public void Result7()
{
    Response.Write("<h1>Result 7</h1>");

    var SQL = (from p in L_tbl_urunler
               let LKod = L_tbl_urunler_kod.Where(k => k.ID == p.KodID).Select(x => x.Kod).FirstOrDefault()
               select new
               {
                   p.Adi,
                   kod = LKod
               }); //result count = 9 record;

    foreach (var i in SQL)
    {
        Response.Write(i.Adi + " - " + i.kod + "<br/>");
    }

    Response.Write("<br/><hr/>");
}

结果1

Urun_1 - Kod_1
Urun_2 - Kod_2
Urun_3 - Kod_3

结果2

Urun_1 - Kod_1
Urun_2 - Kod_2
Urun_3 - Kod_3
Urun_4 - Kod_1
Urun_4 - Kod_2
Urun_4 - Kod_3
Urun_5 - Kod_1
Urun_5 - Kod_2
Urun_5 - Kod_3
Urun_6 - Kod_1
Urun_6 - Kod_2
Urun_6 - Kod_3
Urun_7 - Kod_1
Urun_7 - Kod_2
Urun_7 - Kod_3
Urun_8 - Kod_1
Urun_8 - Kod_2
Urun_8 - Kod_3
Urun_9 - Kod_1
Urun_9 - Kod_2
Urun_9 - Kod_3

结果3

Urun_1 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_2 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_3 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_4 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_5 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_6 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_7 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_8 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]
Urun_9 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,System.String]

结果4

Urun_1 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_2 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_3 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_4 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_5 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_6 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_7 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_8 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]
Urun_9 - System.Linq.Enumerable+WhereSelectListIterator2[test+tbl_urunler_kod,<>f__AnonymousType31[System.String]]

结果5

Urun_1 - Kod_1  
Urun_2 - Kod_2  
Urun_3 - Kod_3  
Urun_4 -  
Urun_5 -  
Urun_6 -  
Urun_7 -  
Urun_8 -  
Urun_9 -

结果6

Urun_1 - { Kod = Kod_1 }  
Urun_2 - { Kod = Kod_2 }  
Urun_3 - { Kod = Kod_3 }  
Urun_4 -  
Urun_5 -  
Urun_6 -  
Urun_7 -  
Urun_8 -  
Urun_9 -

结果7

Urun_1 - Kod_1  
Urun_2 - Kod_2  
Urun_3 - Kod_3  
Urun_4 -  
Urun_5 -  
Urun_6 -  
Urun_7 -  
Urun_8 -  
Urun_9 -

感谢。