Linq to SQL - 不获取特定列

发布于 2024-07-18 22:00:59 字数 239 浏览 5 评论 0原文

有没有办法使用 linqtosql 获取特定列,而不必使用匿名类型并单独指定每个返回的字段?

我们使用 SQLMetal 生成 dbml 文件,其中包含要放入查询数据结果的所有类型。 但是,当 linq 查询中包含选择列时,结果将进入匿名类型,而不是 dbml 文件中声明的类型。 我想从特定表中选择除一列之外的所有列,但仍然以相关 dbml 类型返回结果。

任何想法表示赞赏。

Is there a way to not fetch a specific column using linqtosql without having to use an anonymous type and specify each returned filed individually?

We use SQLMetal to generate the dbml file which contains all the types into which to put the queried data results. However, when select columns are included in the linq query, the results go into an anonymous type instead of the type declared in the dbml file. I would like to select all but one of the columns from a particular table, but still have the results returned in the related dbml type.

Any ideas appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

素染倾城色 2024-07-25 22:00:59

LINQ to SQL 支持延迟加载各个属性。 在 DBML 设计器中,您可以在列属性中将Delay Loaded 设置为true。 延迟加载的列不会包含在初始 SELECT 中。 如果您尝试访问对象的属性并且该对象尚未加载,则将执行另一个 SELECT 语句以从数据库中获取该值。

如果您手动编辑 DBML 文件,请设置 。 如果您手动编写 LINQ to SQL 类,则只需将属性的支持字段声明为 Link 而不是 T 即可。 例如:

[Table]
public class Person
{
    private Link<string> _name;

    [Column(Storage = "_name")]
    public string Name
    {
        get { return _name.Value; }
        set { _name.Value = value; }
    }
}

另请参阅 Scott Guthrie 的这篇文章


更新:如果您希望该列在需要时仍然可用,则上述答案适用。 除非您特别要求(请参阅LoadOptions)或尝试访问它,否则它不会包含在SELECT中。

如果您根本不想使用或访问该列,并且不想将其作为类属性使用,那么请使用 Serapth 的回答:从 DBML 文件中删除该列。 确保您了解其含义,例如该列上并发检查的丢失。

LINQ to SQL supports lazy loading individual properties. In the DBML designer, you can set Delay Loaded to true in a column's properties. Columns that are delay loaded will not be included in the initial SELECT. If you try to access the object's property and it has not been loaded yet, then another SELECT statement will be executed to bring this value from the DB.

If you are editing the DBML file by hand, set <Column IsDelayLoaded="true">. If you are writing your LINQ to SQL classes by hand, it is as simple as declaring the property's backing field as a Link<T> instead of T. For example:

[Table]
public class Person
{
    private Link<string> _name;

    [Column(Storage = "_name")]
    public string Name
    {
        get { return _name.Value; }
        set { _name.Value = value; }
    }
}

See also the "Delay/Lazy Loading" section in this post by Scott Guthrie.


Update: The above answer applies if you want the column to still be available when you need it. It will not be included in SELECTs unless you specifically ask for it (see LoadOptions) or try to access it.

If you just don't want to use or access the column at all, and you don't want it available as a class property, then go with Serapth's answer of removing the column from the DBML file. Be sure you understand the implications, such as the loss of concurrency checking on that column.

初吻给了烟 2024-07-25 22:00:59

如果您非常懒,为什么不通过 DBML 设计器再次添加该表,将其重命名为 MyTableWithOutColumnX,然后删除您不想返回的记录呢? 只需添加列名称并点击删除即可。

它很草率,但在某种程度上,有时不应该出现记录的表也是如此。 最重要的是,它很简单,它全部抽象在 DBML 中,因此除了切换要访问的表之外,它不会影响您的代码。 名字很好,对于在遥远的将来维护您的代码的人来说甚至可能是有意义的。

If you are extremely lazy, why not add the table a second time via DBML designer, rename it MyTableWithOutColumnX then delete the record you don't want returned? Just hilite the column name and hit delete.

Its sloppy, but then, to a certain degree, so is having a table where a record shouldn't appear sometimes. More then anything, it is easy, its all abstracted in the DBML so it shouldnt impact your code, other than switching which table to access. Well named, it might even make sense to someone maintaining your code in the distant future.

孤城病女 2024-07-25 22:00:59

感谢大家的意见。 我最终确定的解决方案是简单地指定我想要带回的列,但要在我想要的类型的新对象中的 Linq 语句中执行此操作...一个示例应该有所帮助!:

表 log 有三列

  • LogID
  • DateLogged
  • SerializedData

但我只想要 DateLogged & 序列化数据。 但是,我希望在 SQLMetal 生成的数据容器内实现这一点,

我通过以下语句实现了这一点:


Dim q = From logItem In dc.Log Select New Log With {.LogID = logItem.LogID, .DateLogged = logItem.DateLogged}


希望对其他人有帮助!

Thanks all for your input. The final solution I have settled on is to simply specify the columns i want to bring back BUT to do this within the Linq statement in a new object of the type I want... an example should help!:

Table log has three colums

  • LogID
  • DateLogged
  • SerializedData

But I only want DateLogged & Serialized data. However, I would like this within a datacontainer generated by SQLMetal

I achieved this with the following statement:


Dim q = From logItem In dc.Log Select New Log With {.LogID = logItem.LogID, .DateLogged = logItem.DateLogged}


Hope that helps someone else out there!

溺ぐ爱和你が 2024-07-25 22:00:59

请查看此链接,了解如何使用 SQLMetal 启用列的延迟加载。

它基本上使用 XSLT 来后处理生成的文件,将列类型从 X 转换为 Link(这两种类型都来自 System.Data.Linq 命名空间)

Look at this link for an example of how to enable lazy loading of columns using SQLMetal.

It basically uses XSLT to post-process the generated file to convert the column type from X to Link<X> (both types from the System.Data.Linq namesapce)

浮世清欢 2024-07-25 22:00:59

LINQ to SQL 知道要包含哪些列的方式是通过您在查询上投影的匿名类型。 我认为没有任何其他方法可以让 LINQ to SQL 排除列,因为直接投影将返回所有内容。

The way that LINQ to SQL knows what columns to include is by the anonymous type you project over the query. I don't think there is any other way to have LINQ to SQL exclude columns as a straight projection will return everything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文