我在 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/