简体   繁体   English

Linq 使用 .net 中的“联接”从具有相同名称的 2 个不同表的 2 列中获取数据?

[英]Linq to get the data from 2 columns having same name of 2 different tables using "Joins" in .net?

  1. This is my method where I emplement the autocomplete-:这是我实现自动完成的方法:
public async Task<List<string>> EmailSearch(string filter)
        {
            try
            {
                var x = await _context.tbl_Account.Where(x => x.Email.Contains(filter) && x.IsActive == true).Select(x => x.Email).ToListAsync();
                return x;
            }
            catch(Exception)
            {
                throw;
            }
        }
  1. In the above code I was getting the data from one table but now I want data from another table also using "Joins" with Linq for condition like in above code, in .NET Core.在上面的代码中,我从一个表中获取数据,但现在我希望另一个表中的数据也使用 Linq 的“联接”,以实现与上述代码类似的条件,在 .NET 核心中。

  2. The table names are tbl_Account and tbl_Contacts both table have a column which is named as Email (same column name).表名是 tbl_Account 和 tbl_Contacts 两个表都有一个名为 Email 的列(相同的列名)。

-> Thanks in advance... -> 在此先感谢...

You create two queries and then union them, since you're just using a string as a result, you should be ok, try something like this...您创建两个查询,然后将它们合并,因为您只是使用字符串作为结果,您应该没问题,尝试这样的事情......

public async Task<List<string>> EmailSearch(string filter)
    {
        try
        {
            var x = _context.tbl_Account
                    .Where(x => x.Email.Contains(filter) && x.IsActive == true)
                    .Select(x => x.Email);
            var y = _context.tbl_Contacts
                    .Where(x => x.Email.Contains(filter) && x.IsActive == true)
                    .Select(x => x.Email);

            var z = await x.Union(y).ToListAsync()
            return z;
        }
        catch(Exception)
        {
            throw;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 postgresql 查询使用连接从两个表中获取总和和列 - postgresql query to get sum and columns from two tables using joins 使用连接但不同条件的相同LINQ查询 - Same LINQ query using joins but different conditions 如何在C#中使用linq从具有相同和不同节点的xml中获取相同的元素值 - How to get same element value from xml which having same and different nodes using linq in c# 使用LINQ从不同的表中拉出不同的列 - Pulling out different columns from different tables using LINQ 当不同表中的两列具有相同名称时,如何使用SqlDataReader读取数据? - How to read data using SqlDataReader when two columns in different tables have same name? linq查询选择两列中具有相同名称但值不同的记录 - linq query to select record having same name but different value in two columns 使用 LINQ 对连接表中的列求和以获取计算值 - Sum Columns from joined tables to get a calculated value using LINQ 如何使用linq从xml获取具有相同名称的不同值 - How to get different values with same name from xml with linq 在MVC中使用Linq将表与不同数据组合在一起? - Combining Tables With Different Data Using Linq in MVC? 从具有相同ID的不同表中减去2列 - Subtracting 2 columns from different tables with the same ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM