我正在使用System.Linq.Dynamic和EntityFramework。我的实体如下:
public class Customer
{
public Customer()
{
CustomerInterests = new List<CustomerInterest>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<CustomerInterest> CustomerInterests { get; set; }
}
public class CustomerInterest
{
public int Id { get; set; }
public int CustomerId { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
}
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
}
以下是我的方法:
public dynamic Get(long customerId)
{
var query = DbContext.Customers.Include("CustomerInterests").Include("CustomerInterests.Course").AsQueryable();
return query.Where(filter => filter.Id == customerId).Select("new(id,name,customerInterests)");
}
JSON结果:
{
"id": 2003,
"name": "name customer",
"customerInterests": [
{
"customerId": 2003,
"courseId": 2,
"course": null,
"id": 2016
},
{
"customerId": 2003,
"courseId": 3,
"course": null,
"id": 2017
}
]
}
我正在尝试加载属性Course
,但它总是会返回null
,因为您可以在JSON结果中看到。
如何创建选择器new(....)
以正确加载属性Course
。我已经尝试new (customerInterests.course) as customerInterests.course
但没有成功。
不要忘记我正在尝试导航Customer
(对象) - &gt; CustomerInterests
(收藏) - &gt;对于每个项目加载Course
(对象)。
如果你能帮我解决这个问题,我将不胜感激。
答案 0 :(得分:1)
我以前遇到过这种情况,我的结论是:在Lambda扩展方法中,您只能加载第一级相关对象。 您可以获得customerInterests列表,但是您无法获得此列表的外国记录。 但您可以使用LINQ查询。
file = "log"..os.date("%S.%M.%H%p.%d.%m.%Y")..".txt"
do
log = io.open(file, "w")
io.output(log)
end
dechexint = remainder
function hexTracker()
remainder = result % 16
result = result / 16
print(remainder, "\n")
io.write(remainder, "\n")
end
function hexTranslator()
if remainder == 11
then print("A")
io.write("A")
else
if dechexint == 12
then print("B")
io.write("B")
else
if dechexint == 13
then print("C")
io.write("C")
else
if dechexint == 14
then print("D")
io.write("D")
else
if dechexint == 15
then print("E")
io.write("E")
else
if dechexint == 16
then print("F")
io.write("F")
end
end
end
end
end
end
end
decint = io.read("*number")
result = decint / 16
result = decint % 16
while(result>0.1)
do
hexTracker()
hexTranslator()
end
<强> EDITED 强> 如果您正在使用EF7,则可以使用ThenInclude方法加载第二级外部记录
input:
4579
output:
3
0.1875