使用 LINQ to SQL 从单个记录返回多个对象
我有一个存储过程,它返回这样的记录(我简化了它)
SELECT
Id, FirstName, LastName, Father.FirstName, Father.LastName
FROM Profiles
Left JOIN Profiles Father on Profiles.FatherId = Father.Id
我希望该记录作为对象的 2 个属性返回
var result = dataContext.StoredProcedure();
Profile = result.Profile;
Father = result.Father;
我的问题是,我如何获取单个记录
SELECT
Id, FirstName, LastName, Father.FirstName, Father.LastName
并将其一半拆分为一个属性/对象(配置文件),另一半成为另一个属性/对象(父亲)
使用存储过程的原因 实际的sql语句包括全文搜索,linq不支持全文搜索,因此我无法使用普通的linq查询。
I have a stored procedure that returns a record like this (I simplified it)
SELECT
Id, FirstName, LastName, Father.FirstName, Father.LastName
FROM Profiles
Left JOIN Profiles Father on Profiles.FatherId = Father.Id
I want this record to be returned as 2 properties of an object
var result = dataContext.StoredProcedure();
Profile = result.Profile;
Father = result.Father;
My question is, how do i take the single record
SELECT
Id, FirstName, LastName, Father.FirstName, Father.LastName
And split half of it to become one property/object (Profile), and the other half to become the other property/object (Father)
Reason For Using a Stored Procedure
The actual sql statement includes a Full Text search, which is not supported in linq, so i cannot use a normal linq query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,你需要两个强类型对象,“Profile”和“Profile”。 SQL 结果集中的“父亲”。我看到有两种选择可以做到这一点;
1>从存储过程中获取结果并创建对象,如下所述;
2>在存储过程中,首先获取表变量中的结果,然后从此表变量中选择两个结果,第一个是“个人资料”,第二个是“父亲”。调用存储过程的 LINQ to SQL 方法应设置为允许多个结果集。然后您可以访问结果,例如;
If I understand your problem correctly, you need two strongly typed objects, 'Profile' & 'Father' from your SQL result set. I see two options to do this;
1> Get your results as is from the stored proc and create your objects as stated below;
2> In your stored proc, get the results in a table variable first and then select two results from this table variable, first one 'Profile', second 'Father'. Your LINQ to SQL method calling the stored proc should be set to allow multiple result-sets. You can then access the results like;