C#SQL语句转换为LINQ如何将此语句转换为有效的linq

时间:2010-03-05 22:53:04

标签: c# asp.net sql linq datatable

我遇到了麻烦这个我有3个数据表我一遍又一遍地使用它们被缓存我想写一个LINQ语句,它可以做到以下这可能吗?

T-SQL VERSION:

SELECT P.[CID],P.[AID]
 ,B.[AID], B.[Data], B.[Status], B.[Language]
 FROM MY_TABLE_1 P
 JOIN 
  (
   SELECT A.[AID], A.[Data], A.[Status], A.[Language] FROM MY_TABLE_2 A  
   UNION ALL
   SELECT B.[AID], B.[Data], B.[Status], B.[Language] FROM MY_TABLE_3 B
  ) B on P.[AID] = B.[AID]
WHERE B.[Language] = 'EN' OR  B.[Language] = 'ANY' AND B.STATUS = 1 AND B.[Language] = 'EN' OR  B.[Language] = 'ANY' AND B.STATUS = 1

然后我希望它创建以下

的结果集

结果: | CID | AID | DATA | STATUS |语言

3 个答案:

答案 0 :(得分:4)

试试这个:

from p in Context.MyTable1
join b in Contact.MyTable2.Concat(Contact.MyTable3)
  on p.aid equals b.aid
where b.Language == "EN" || b.Language == "ANY"
where b.Status == 1
select new
{
    p.CID,
    p.AID,
    b.Data,
    b.Status,
    b.Language
};

答案 1 :(得分:2)

不要这样做。

你的两个选择

  1. 创建一个代表您的Union语句的视图(表2和表3)
  2. 在Table1和AID列上的新视图之间的DBML上创建关系。
  3. 选择SelectMany以获得所需的退货结果。
  4. 或(和首选)

    • 创建一个接受语言/状态的存储过程(假设它们是参数)并返回此数据集。这将是最有效的方法。

    您正在使用业务逻辑进行数据库工作!使用数据库来实现它的目的。

答案 2 :(得分:1)

确保引用System.Data.DataSetExtensions,并使用AsEnumerable()方法使用LINQ to DataSet。

            var myTable1 = new [] { 
            new { CID = "123", AID = 345, Data = 32323, Status = 1, Language = "EN"},
            new { CID = "231", AID = 123, Data = 11525, Status = 2, Language = "EN"},
            new { CID = "729", AID = 513, Data = 15121, Status = 1, Language = "ANY"},
            new { CID = "231", AID = 123, Data = 54421, Status = 2, Language = "EN"}}
            .ToDataTable().AsEnumerable();

        var myTable2 = new [] { 
            new { CID = "512", AID = 513, Data = 32323, Status = 1, Language = "ANY"},
            new { CID = "444", AID = 123, Data = 11525, Status = 2, Language = "BLAH"},
            new { CID = "222", AID = 333, Data = 15121, Status = 1, Language = "ANY"},
            new { CID = "111", AID = 345, Data = 54421, Status = 2, Language = "EN"}}
            .ToDataTable().AsEnumerable();

         var myTable3 = new [] { 
            new { CID = "888", AID = 123, Data = 32323, Status = 2, Language = "EN"},
            new { CID = "494", AID = 333, Data = 11525, Status = 1, Language = "FR"},
            new { CID = "202", AID = 513, Data = 15121, Status = 1, Language = "EN"},
            new { CID = "101", AID = 345, Data = 54421, Status = 2, Language = "ANY"}}
            .ToDataTable().AsEnumerable();

         var q = from p in myTable1
                 join b in myTable2.Union(myTable3) on p.Field<int>("AID") equals b.Field<int>("AID")
                 where (b.Field<string>("Language") == "EN" || b.Field<string>("Language") == "ANY") && b.Field<int>("Status") == 1
                 select new
                 {
                     CID = p.Field<string>("CID"),
                     B_AID = p.Field<int>("AID"),
                     P_AID = b.Field<int>("AID"),
                     Data = b.Field<int>("Data"),
                     Status = b.Field<int>("Status"),
                     Language = b.Field<string>("Language")
                 };


         var table = q.ToDataTable();

我使用了一个扩展方法,你可以找here来测试它,如果你在DataTables上做了很多LINQ,它会非常有用。