开发者

How to Use Multiple 'IN' on a DataTable

I have asked how to use the 'IN' here.

So far, this SQL Server query:

select * 
  from table 
  where column1 in 
    (
        select column2 
        from table
 开发者_StackOverflow中文版   )

can be translated as:

table.Select(
    string.Format("column1 in ({0})",
    string.Join(",", table Rows.OfType<DataRow>()
        .Select(r=>r["column2"].ToString())
        .Distinct())));

My question is how to translate this SQL query:

select column3
  from table
  where column1 in 
    (
        select column2
        from table
    )

So that I can use what's inside on column3 on another 'IN'.

(e.g.)

select columnA
  from table2 
  where columnB in 
    (
        select column3 
        from table1 
        where column1 in 
            (
                select column2 
                from table1
            )
    )


Linq is a tool. It has its strengths and its limitations.

What I would do is use a custom query to accomplish this. Let SQL Server do the brunt work here, not Linq. It'll dramatically improve the performace, as well.

var sqlTxt = "select columnA "  +
    "from table2 "  +
    "where columnB in "  +
    "( select column3 "  +
    " from table1 "  +
    " where column1 in " +
        "(select column2 " + 
            "from table1) " +
    ")"

results = ExecuteQuery(sqlTxt);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜