Linq to sql返回复杂对象 [英] Linq to sql returns complex objects

查看:81
本文介绍了Linq to sql返回复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在使用带有N层的linq到sql,我需要将复合对象从商务层返回到表示层

我用这种方式

Hello ,
I''m using linq to sql with N-Tiers and i need to returns composite objects from Buisness Layer to Presntation layer

I used this Way

public static IQueryable GetComplexResult(int companyId)
    {
        DataClassesDataContext dataContext = new DataClassesDataContext();
        var query = from c in dataContext.Companies
                    join x in dataContext.Positions
                    on c.CreatorPositionId equals x.PositionId
                    select new
                    {
                        c.CompanyName,
                        x.PositionName
                    };
        return query.AsQueryable();
    }




代码后页




Code behinde page

IQueryable memberCompanies = Company.GetCompanyWorkingSectorAndServices(MemberSession.MemberId);
        RepeaterCompanies.DataSource = memberCompanies;
        RepeaterCompanies.DataBind();



1-这是最好的方法吗?
2-性能如何?

我不想创建其他类以返回值

谢谢大家:)



1-Is this the best way to do it?
2-What about the performance ?

I don''t want to create additional classes to return the value

Thank you all :)

推荐答案



如果您的值是固定的,并且不会经常更改,则只需将其转换为 ToList 并在各层之间发送即可.选择CompanyName和PositionName后,可以将其转换为ToList并将其传递到UI上进行解析.

您还可以使用 List< Dynamic> (在Dot net框架4.0中引入)返回对象.唯一的缺点是,您将无法获得IntelliSense.
Hi,

If your value is fix and it will not be change frequently then you can just convert it as ToList and send it between layers. As you have CompanyName and PositionName selection, you can convert it into ToList and pass it on UI for parsing.

You can also use List<Dynamic>(Introduce in Dot net framework 4.0) to return your object. The only disadvantage is, you will not get IntelliSense for that.


这可以帮助您验证方法:
http://msdn.microsoft.com/en-us/library/bb882671.aspx [ ^ ]
Let this help you to validate your approach:
http://msdn.microsoft.com/en-us/library/bb882671.aspx[^]


这篇关于Linq to sql返回复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆