我都是C#,Linq和.Netcore的新手,我想做的就是将sql中的以下查询转换为linq
SELECT
p.policyId,
p.insuranceId,
p.policyType,
p.coverageType,
p.price,
us.FirebaseUID
FROM Policies p
JOIN Users us ON p.userId = us.FirebaseUID
我的策略是父类ExampleRes内的嵌套类,我遵循此question答案以填充策略列表,但我收到此错误:
Error CS1061: 'ExampleRes.PoliciesInfo' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'ExampleRes.PoliciesInfo' could be found (are you missing a using directive or an assembly reference?)
这是我正在使用的查询
public List<ExampleRes> GetUserInfo(string fuid)
{
var policies = (from pol in _context.Policy
join us in _context.User
on pol.userId equals us.FirebaseUID
select new ExampleRes
{
userId = us.userId,
fuid = us.FirebaseUID,
policy = (new ExampleRes.PoliciesInfo
{
policyId = pol.policyId,
policyType = pol.policyType,
policyPrice = pol.price,
coverageType = pol.coverageType
}).toList()
}).toList();
return policies;
}
这是 ExampleRes类
public class ExampleRes
{
public ExampleRes()
{
}
// User info
public int userId { get; set;}
public string fuid { get; set; }
public List<PoliciesInfo> policy { get; set; }
// Policies info
public class PoliciesInfo{
public int policyId { get; set; }
public int policyType { get; set; }
public int coverageType { get; set; }
public int policyPrice { get; set; }
}
}
答案 0 :(得分:2)
您的查询应如下所示:
var policies = (from pol in _context.Policy
join us in _context.User
on pol.userId equals us.FirebaseUID
select new ExampleRes
{
userId = us.userId,
fuid = us.FirebaseUID,
policy = new List<ExampleRes.PoliciesInfo> {
new ExampleRes.PoliciesInfo
{
policyId = pol.policyId,
policyType = pol.policyType,
policyPrice = pol.price,
coverageType = pol.coverageType
}
}
}).ToList();
已更新:
我误解了这个问题,应该是这样的。但是我相信应该有一个更好的解决方案。
var policies = (from us in _context.User
select new ExampleRes
{
userId = us.userId,
fuid = us.FirebaseUID,
policy = (
from pol in _context.Policy
where pol.userId == us.FirebaseUID
select new ExampleRes.PoliciesInfo
{
policyId = pol.policyId,
policyType = pol.policyType,
policyPrice = pol.price,
coverageType = pol.coverageType
}
).ToList()
}).Where(u => u.policy.Count > 0).ToList();
再次更新
@Ilan Keshet感谢将linq函数查询与非函数查询结合在一起是一个坏习惯。这是我修改后的解决方案:
var policies = (from pol in _context.Policy
group pol by pol.userId into uid
where uid.Count() > 0
join us in _context.User
on uid.Key equals us.FirebaseUID
select new ExampleRes
{
userId = us.userId,
fuid = us.FirebaseUID,
policy = (from pol in _context.Policy
where pol.userId == uid.Key
select new ExampleRes.PoliciesInfo
{
policyId = pol.policyId,
policyType = pol.policyType,
policyPrice = pol.price,
coverageType = pol.coverageType
}).ToList()
}).ToList();
答案 1 :(得分:1)
我认为您正在寻找Nested query
尝试这样的事情:
var policies = (from pol in _context.Policy
join us in _context.User
on pol.userId equals us.FirebaseUID into policies
select new ExampleRes
{
userId = us.userId,
fuid = us.FirebaseUID,
policy = (from p in policies
select new ExampleRes.PoliciesInfo
{
policyId = p.policyId,
policyType = p.policyType,
policyPrice = p.price,
coverageType = p.coverageType
}).ToList()
}).ToList();
或
var policies = (from us in _context.User
select new ExampleRes
{
userId = us.userId,
fuid = us.FirebaseUID,
policy = _context.Policy
.Where(pol=>pol.userId == us.FirebaseUID)
.Select(new ExampleRes.PoliciesInfo
{
policyId = pol.policyId,
policyType = pol.policyType,
policyPrice = pol.price,
coverageType = pol.coverageType
}).ToList()
}).ToList();
答案 2 :(得分:0)
您不能在ToList()
上呼叫ExampleRes.PoliciesInfo
,而不是任何类型的Enumerable
policy = (new ExampleRes.PoliciesInfo
{
policyId = pol.policyId,
policyType = pol.policyType,
policyPrice = pol.price,
coverageType = pol.coverageType
}).toList() // <== doesn't make sense
简而言之,您需要使用实例化的类作为元素创建一个新的列表
new List<blah> { new blah() {...} }
Enumerable.ToList Method (IEnumerable)
从IEnumerable创建列表。
答案 3 :(得分:0)
首先toList()
(小t
)将导致编译时错误。将其更改为ToList()
(大写T
)。
第二,policy
属性是List<PoliciesInfo>
(policysinfo的列表),您设置了错误的内容。
您应该做的是这样:
policy = new List<Examples.PoliciesInfo> {
new Examples.PoliciesInfo {
policyId = pol.policyId,
policyType = pol.policyType,
policyPrice = pol.price,
coverageType = pol.coverageType
}
// you can create more Examples.PoliciesInfo here, just seperate them with comma
}.ToList()
这将解决。