c# - Linq加入c#

标签 c# .net linq-to-entities

我在 Linq 工作的 friend 。我在 linq 查询中使用实体模型,如下所示。

var Records = from Cats in Context.Categories
join prod in Context.Products on Cats.Id equals prod.Category_Id
select new { CatName = Cats.Name, ProdName = prod.Name };

我想转换对象列表中的 Record var,因此我创建了一个包含两个实体值(产品、类别)的中间对象。现在,当我将此 var 转换为列表时,如

List<test> testList = (List<test>)Records;

作为 Record.ToList();是编译器错误。我如何将 var 对象转换为 list 以便在前端将其与 listview 绑定(bind)。 lambda 中是否有任何替代方案,我们也将不胜感激。我的做法对吗?

我的测试类如下:

class test{
string catname;
string productname;
}

最佳答案

在您的查询中使用 ToList()

var Records = (from Cats in Context.Categories
               join prod in Context.Products on Cats.Id equals prod.Category_Id
               select new test { CatName = Cats.Name, ProdName = prod.Name }).ToList();

为了让它工作,你需要定义你的测试类如下(你需要定义属性)

public class test {
   public string catname {get;set;}
   public string productname {get;set;}
}

关于c# - Linq加入c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19180077/

相关文章:

c# - 将 UpdateDefinition<TEntity> 用于 MongoDB 通用存储库

c# - 无需 PredicateBuilder 将动态谓词添加到表达式

林克 |如何在不分组的情况下获得 SUM?

c# - 是否可以使用 C# 在 excel 工作簿中编写功能

c# - "Cleanly"使用 LINQ to SQL Server 部署 ASP.NET 应用程序

c# - 有没有办法检测用户何时在 Blazor Server 中关闭浏览器?

c# - 动态母版页,可以访问两个母版中的变量

c# - 如何获取 Azure CloudBlobContainer 的大小

c# - 是否有 C# 不区分大小写的等于运算符?

c# - Linq 通过两个属性选择不同