使用 linq 合并两个表

发布于 2024-12-26 11:42:55 字数 937 浏览 4 评论 0原文

我有两个 linq - sql 查询,我想知道如何加入它们。

第一个查询

 var ab = from a in Items_worker.getCEAItems()
                where a.ProjectCode == lbl_projectCode.Text
                select new
                {
                    a.ID
                };

第二个查询

 var j = from c in tblInc_worker.get(c => c.MarginID == MarginID && c.IncTypeID == "CAPEX")
                orderby c.DateCreated
                select c.ID;

第一个查询将返回:

fasf-1212-1212-1212-1212

afaa-1414-1414-1414-1414

第二个查询将返回:

fasf -1212-1212-1212-1212

afaa-1414-1414-1414-1414

0000-0000-0000-0000-0000

1111-1111-1111-1111-1111

问题是我如何连接这两个表。其中第二个查询应返回第一个查询中找到的具有相同 ID 的所有记录,加上第二个查询中包含“0000-0000-0000-0000-0000”的 id。

结果应为:

fasf-1212-1212-1212 -1212

afaa-1414-1414-1414-1414

0000-0000-0000-0000-0000

i have two linq - sql queries, and im wondering how to join them..

First Query

 var ab = from a in Items_worker.getCEAItems()
                where a.ProjectCode == lbl_projectCode.Text
                select new
                {
                    a.ID
                };

Second Query

 var j = from c in tblInc_worker.get(c => c.MarginID == MarginID && c.IncTypeID == "CAPEX")
                orderby c.DateCreated
                select c.ID;

First Query would return:

fasf-1212-1212-1212-1212

afaa-1414-1414-1414-1414

Second Query would return:

fasf-1212-1212-1212-1212

afaa-1414-1414-1414-1414

0000-0000-0000-0000-0000

1111-1111-1111-1111-1111

question is how can i possibly join the two table. Wherein the second query should return all of the records with the same ID found in the first query plus the id containing "0000-0000-0000-0000-0000" second query..

The result should be:

fasf-1212-1212-1212-1212

afaa-1414-1414-1414-1414

0000-0000-0000-0000-0000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2025-01-02 11:42:55

您可以使用 union 来连接两个查询,例如将第二个查询分成两部分,条件如下:

var ab = from a in Items_worker.getCEAItems() 
            where a.ProjectCode == lbl_projectCode.Text 
            select new 
            { 
                a.ID 
            }; 


var j = from c in tblInc_worker.get(c => c.MarginID == MarginID && c.IncTypeID == "CAPEX") 
            orderby c.DateCreated 
            select c.ID where c.ID.Equals("0000-0000-0000-0000-0000"); 

var j1 = from c in tblInc_worker.get(c => c.MarginID == MarginID && c.IncTypeID == "CAPEX") 
            orderby c.DateCreated 
            select c.ID where !(c.ID.Equals("0000-0000-0000-0000-0000"));

var result = ab.Union(j.Union(j1));

希望这有帮助..

You can use union to join the both queries, for example split your second query in two with conditions like :

var ab = from a in Items_worker.getCEAItems() 
            where a.ProjectCode == lbl_projectCode.Text 
            select new 
            { 
                a.ID 
            }; 


var j = from c in tblInc_worker.get(c => c.MarginID == MarginID && c.IncTypeID == "CAPEX") 
            orderby c.DateCreated 
            select c.ID where c.ID.Equals("0000-0000-0000-0000-0000"); 

var j1 = from c in tblInc_worker.get(c => c.MarginID == MarginID && c.IncTypeID == "CAPEX") 
            orderby c.DateCreated 
            select c.ID where !(c.ID.Equals("0000-0000-0000-0000-0000"));

var result = ab.Union(j.Union(j1));

Hope this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文