DataContext - 通过 LINQ 查询时自动加载可选引用值
我有以下情况(例如)
我有一个名为:Master.dbml 的 DataContext
它有 2 个表:
- Hobby {id;name;}
- HobbyReference {id;Hobby_Name;Hobby_Good_Name;}
当我查询 Hobby 时,DataContext 应该检查是否(伪代码):
Hobby.Name EXISTS in HobbyReference.Hobby_Name
THEN
take HobbyReference.Hobby_Good_Name
ELSE
take Hobby.Hobby_Name
END IF
对此的最佳实践是什么?
我知道如何做到这一点(扩展数据上下文),但我不知道如何完全实现它。
我该怎么做?
I have the following situation (as example)
I have a DataContext named : Master.dbml
It has 2 tables:
- Hobby {id;name;}
- HobbyReference {id;Hobby_Name;Hobby_Good_Name;}
Always when i query Hobby, the DataContext should check if (pseudo-code):
Hobby.Name EXISTS in HobbyReference.Hobby_Name
THEN
take HobbyReference.Hobby_Good_Name
ELSE
take Hobby.Hobby_Name
END IF
What's the best practice arround this?
I have an idea of how to do it (extending the datacontext), but i don't know how to fully implement it.
How would i do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Hobby.Hobby_Name
不存在!无论如何,要在从 datacontext 返回之前进行检查,您可以执行以下操作:
Hobby.Hobby_Name
not exists!anyway to check before return from datacontext you can do the following:
如果 HobbyReference 上的每个名称从来没有超过一个条目,并且如果这是用于查询(即不是用于更新),那么您可以执行类似这样的操作,
这应该允许您执行子查询,例如
它将返回 AmishedHobby 类,其中,因为它没有链接到现有表,意味着更改不会保留回数据库。
If there is never more than one entry on HobbyReference for each name, and if this is used for querying (ie not for updating) then you can do something like
This should allow you to do subqueries such as
It will return the AmendedHobby class which, as it is not linked to an existing table, means that changes won't be persisted back to the database.