c# - 获取 linq 返回 IEnumerable<DataRow> 结果

标签 c# asp.net linq linq-to-sql c#-4.0

如何将以下 SQL 查询转换为 C# 中的 LINQ?

我不需要两个表中的所有列,结果集应该是

IEnumerable<DataRow>

查询:

select 
    c.level, cl.name, c.quantity
from 
    dbo.vw_categories c
left join 
    dbo.vw_categoriesLocalization cl on c.level = cl.level 
                                     and cl.language = 'en'
where 
    c.level like '000%' 
    and c.level <> '000';

预期:

IEnumerable<DataRow> result = ????

提前致谢..

最佳答案

以下是编写查询的方法:

var query =
    from c in db.vw_categories
    join cl in db.vw_categoriesLocalization on c.level equals cl.level into clo
    from cl in clo.DefaultIfEmpty()
    where (cl == null || cl.language == "en")
          && c.level.StartsWith("000")
          && c.level != "000"
    select new
    {
        c.level,
        name = cl == null ? null : cl.name,
        c.quantity
    }

将其转换为 IEnumerable<DataRow>也许你可以做这样的事情:

var datarows = query.Select(r => {
    var row = dataTable.NewRow();
    row["level"] = r.level;
    row["name"] = r.name;
    row["quantity"] = r.quantity;
    return row;
});

关于c# - 获取 linq 返回 IEnumerable<DataRow> 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4503517/

相关文章:

c# - C# 中用于字符等价物的正则表达式

c# - 使用 .Net Core 3.0 访问 SerialPort 类

c# - Action 的通用约束没有按预期工作

Linq 将整数子列表与整数列表相交

c# - 动态 Lambda 表达式调用

c# - autocad .net在不使用ObjectId的情况下存储和检索对象ID

asp.net - 尝试学习 ASP.NET 的 WinForms/WPF 开发人员资源

asp.net - 将主机与 SQL 分开后出现错误 "Your connection to this site is not private!"

asp.net - "This operation requires IIS integrated pipeline mode."

c# - Linq Join 将所有字段保留在一侧而不显式列出