LINQ-不选择某些字段? [英] LINQ - NOT selecting certain fields?

查看:53
本文介绍了LINQ-不选择某些字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与实体框架映射的LINQ查询,如下所示:

I have a LINQ query mapped with the Entity Framework that looks something like this:

image = this.Context.ImageSet
                    .Where(n => n.ImageId == imageId)
                    .Where(n => n.Albums.IsPublic == true)
                    .Single();

这将返回一个图像对象并按预期工作.

This returns a single image object and works as intended.

但是,此查询返回数据库中我的Image表的所有属性. 在正常情况下,这会很好,但是这些图像包含大量二进制数据,需要很长时间才能返回.

However, this query returns all the properties of my Image table in the DB. Under normal circumstances, this would be fine but these images contain a lot of binary data that takes a very long time to return.

基本上,在当前状态下,我的linq查询正在执行:

Basically, in it current state my linq query is doing:

Select ImageId, Name, Data
From Images
...

但是我需要一个执行此查询的查询:

But I need a query that does this instread:

Select ImageId, Name
From Images
...

请注意,我想加载除数据外的所有内容. (我可以在第二次异步传递中获得此数据)

Notice i want to load everything except the Data. (I can get this data on a second async pass)

推荐答案

不幸的是,如果使用LINQ to SQL,则没有最佳解决方案.

Unfortunately, if using LINQ to SQL, there is no optimal solution.

您有3个选择:

  1. 您将返回带有上下文跟踪的实体,以及本例中包含所有字段的所有图片(在本例中为Image)
  2. 您选择字段并返回匿名类型
  3. 您选择自己的字段并返回一个强类型的自定义类,但是如果您想要的话,则会丢失跟踪.

我喜欢LINQ to SQL,但这就是事实.

I love LINQ to SQL, but thats the way it is.

对您来说,我唯一的解决方案是重组数据库,并将所有大数据移到单独的表中,然后从图像"表中链接到该表.

这样,在返回Image时,您只需在新的DataID字段中返回一个键,然后您就可以在需要时以及需要时访问该较重的数据.

This way when returning Image you'd only return a key in the new DataID field, and then you could access that heavier Data when and if you needed it.

欢呼

这篇关于LINQ-不选择某些字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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