使用 LINQ to SQL 从单个记录返回多个对象

发布于 2024-11-04 05:08:13 字数 600 浏览 1 评论 0原文

我有一个存储过程,它返回这样的记录(我简化了它)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-11-11 05:08:13

如果我正确理解你的问题,你需要两个强类型对象,“Profile”和“Profile”。 SQL 结果集中的“父亲”。我看到有两种选择可以做到这一点;

1>从存储过程中获取结果并创建对象,如下所述;

Profile p = new Profile P { Id=result.Id, FirstName= result.FirstName, LastName=result.LastName}; 
//and similar code for creating the 'Father' object

2>在存储过程中,首先获取表变量中的结果,然后从此表变量中选择两个结果,第一个是“个人资料”,第二个是“父亲”。调用存储过程的 LINQ to SQL 方法应设置为允许多个结果集。然后您可以访问结果,例如;

Profile = result.Profile;
Father = result.Father;

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;

Profile p = new Profile P { Id=result.Id, FirstName= result.FirstName, LastName=result.LastName}; 
//and similar code for creating the 'Father' object

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;

Profile = result.Profile;
Father = result.Father;
维持三分热 2024-11-11 05:08:13
  • 创建具有 2 个属性的强类型对象:Profile、Father
  • 为 Profile 对象创建导航属性 Father,以便接收 Profile 的消费者可以轻松导航到 Father
  • Create strongly-type object with 2 properties: Profile, Father
  • Create a navigation property Father for Profile object, so receiving Profile a consumer could easy navigate to Father
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文