c# - 如何使用linq语句进行查询?

标签 c# linq

我有两个对象

List<Report> 
List<Newsletter>

我需要某些属性,例如

Id
Date
Status
text

来自两个列表。有没有办法合并这些列表并使用 linq 语句查询它以将特定属性作为单独的列表返回? 请帮忙。

对象结构如下:

Report
{
    int id,
    datetime reportDate, 
    enum Status,
    string text
};

Newsletter
{
    int id, 
    datetime newsletterDate,
    string Status,
    string text
};

最佳答案

var items = 
      reportList.Select(x =>  new { x.Id, x.Date, x.Status, x.text })
      .Concat(
        newsList.Select(x =>  new { x.Id, x.Date, x.Status, x.text }) );

更新,均衡状态属性:

var items = 
      reportList.Select(x =>  new { x.Id, x.Date, Status = x.Status.ToString(), x.text })
      .Concat(
        newsList.Select(x =>  new { x.Id, x.Date, Status, x.text }) );

关于c# - 如何使用linq语句进行查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871294/

相关文章:

c# - 在 SharpDevelop 中启用 sln 项目的设计模式

c# - 命令模式和 OnPaint 事件问题

c# - Win 7 中的 "pin to desktop",兼容 XP

c# - 反射(reflect) ObjectContext 中的所有更改而不持久化到数据库

c# - 通过两个属性从 IQueryable 中排除复杂对象列表

c# - 通过 id linq to xml C# 选择一个对象

c# - 变量声明未按预期解析

c# - 了解 WinDbg 输出

c# - LINQ 选择新错误。无法访问列表

c# - 表达式调用和计数