[英]LINQ Query to get data from another table
I have two tables 我有两张桌子
I am trying to write a LINQ Query where I want to select everything in Table 1 but instead of CaseStatusID, I want to return the actual status (Corresponding to CaseStatusID in Table 2). 我试图编写一个LINQ查询,我想在其中选择表1中的所有内容,但要返回实际状态(与表2中的CaseStatusID相对应),而不是CaseStatusID。
I have the query but I the error Cannot convert expression type 'System.Collections.Generic.List<CaseId:int, Status:string>' to return type 'System.Collections.Generic.List<Data.Cases>'
我有查询,但出现错误
Cannot convert expression type 'System.Collections.Generic.List<CaseId:int, Status:string>' to return type 'System.Collections.Generic.List<Data.Cases>'
I have not been able to find a good example to help me. 我一直找不到能够帮助我的好榜样。 How can I achieve this?
我该如何实现?
public List<Case> GetAllCases()
{
var result = (from c in _Context.Cases
join s in _Context.CaseStatus on c.CaseStatusId
equals s.CaseStatusId
select new { c.CaseId, c.CaseStatu.Status }).ToList();
return result;
}
Your return type doesn't match the declared return type List<Case>
. 您的返回类型与声明的返回类型
List<Case>
不匹配。 When you select new {}
, you are actually returning an anonymous object from your linq expression. 当
select new {}
,实际上是从linq表达式返回一个匿名对象。 You can fix it like this: 您可以这样解决:
public List<Case> GetAllCases()
{
var result = (from c in _Context.Cases
join s in _Context.CaseStatus on c.CaseStatusId
equals s.CaseStatusId
select new Case { CaseId = c.CaseId, CaseStatus = c.CaseStatu.Status }).ToList();
return result;
}
Note the new Case{}
when selecting your data. 选择数据时,请注意
new Case{}
。 You may need to change CaseId
and CaseStatus
properties to match the actual properties from the Case
class. 您可能需要更改
CaseId
和CaseStatus
属性,以匹配Case
类的实际属性。
Update : To Clarify, you would need a Case
class that looked minimally like this: 更新 :要澄清,您将需要一个
Case
类,其看起来至少像这样:
public class Case {
public int CaseId { get; set; }
public string CaseStatus { get; set; }
}
I think the problem is in the select statement where you should return objects list of Case: 我认为问题出在应该返回Case的对象列表的select语句中:
public List<Case> GetAllCases()
{
var result = (from c in _Context.Cases
join s in _Context.CaseStatus on c.CaseStatusId
equals s.CaseStatusId
select new Case () { CaseId = c.CaseId, CaseStatusId = c.CaseStatusId }).ToList();
return result;
}
you are creating list of annonymous type and returning list of case. 您正在创建匿名类型列表并返回案例列表。 that is the issue
这就是问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.