Linq查询多行子查询(不加入)

时间:2016-06-06 07:42:27

标签: c# linq

我想像在MySql中那样使用多行子查询进行查询但是我想要没有join ,所以我的SQL查询是:

select * from html_files_tab where html_files_tab.encoded_id in
(select html_src_files_relationsip_tab.file_fk from html_src_files_relationsip_tab where html_src_files_relationsip_tab.src_fk == '1' );

我试过了:

var fileBeans = ( from srcFileTab in conn.Table<FileBean>() where 
                  srcFileTab.fileId in ( from srcFileRelationSipTab in 
                  conn.Table<HtmlSrcFileRelationship>() where 
                  srcFileRelationSipTab.srcFk == encodedSrcId select               
                  srcFileRelationSipTab.fileFk ) select srcFileTab ).ToList();

但是我遇到了一些错误,有没有办法在没有加入的情况下做到这一点?

P.S。使用连接方法:

   var fileBeans = ( from fileTab in conn.Table<FileBean>()
                                  join relationTab in conn.Table<HtmlSrcFileRelationship>()
                                  on fileTab.fileId equals relationTab.fileFk
                                  where relationTab.srcFk == encodedSrcId && relationTab.srcFk == encodedSrcId
                                  select fileTab ).ToList();

使用join我得到一个例外:

Exception thrown: 'System.NotSupportedException' in SQLite.Net.dll

完整堆栈跟踪:

   at SQLite.Net.TableQuery`1.GenerateCommand(String selectionList)
   at SQLite.Net.TableQuery`1.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HiTech.DataBase.selectFileBeansBySrc(String encodedSrcId)

4 个答案:

答案 0 :(得分:1)

您可以使用嵌套迭代的LinQ选项,而不是尝试在LinQ中重新创建SQL,这与子查询几乎相同。

var tabs = (from srcFileTab in conn.Table<FileBean>()
            from srcFileRelationSipTab in conn.Table<HtmlSrcFileRelationship>() 
            where srcFileRelationSipTab.srcFk == encodedSrcId 
               && srcFileTab.fileId == srcFileRelationSipTab.fileFk
            select srcFileTab ).ToList();

答案 1 :(得分:0)

  

有没有办法在没有加入的情况下做到这一点?

您可以使用Any扩展程序并执行此操作。

conn.Table<FileBean>()
    .Select(x=>  conn.Table<HtmlSrcFileRelationship>()
                     .Any(r=> r.srcFk == encodedSrcId  && r.srcFk  == x.encoded_id)) 
                     // not sure it is encoded_id or encodedSrcId in above statement. Update accordingly.  
    .ToList();

答案 2 :(得分:0)

你不能在两个单独的查询中完成吗?

var fileRel = (from srcFileRelationSipTab in 
              conn.Table<HtmlSrcFileRelationship>() where 
              srcFileRelationSipTab.srcFk == encodedSrcId 
              select srcFileRelationSipTab.fileFk).ToList();

var fileBeans = ( from srcFileTab in conn.Table<FileBean>() where 
              srcFileTab.fileId in fileRel 
              select srcFileTab ).ToList();

答案 3 :(得分:0)

SQL IN运算符的直接LINQ等价物是Contains方法:

var fileBeans = 
    (from srcFileTab in conn.Table<FileBean>()
     where (from srcFileRelationSipTab in conn.Table<HtmlSrcFileRelationship>()
            where srcFileRelationSipTab.srcFk == encodedSrcId
            select srcFileRelationSipTab.fileFk).Contains(srcFileTab.fileId)
     select srcFileTab).ToList();