Linq 选择字段

发布于 2024-10-20 21:21:24 字数 357 浏览 0 评论 0原文

只是尝试使用 Linq:

    DataClassesDataContext db = new DataClassesDataContext();
    var q = from p in db.tblBlogEntries select p;
    foreach (var customer in q)
    {
        Response.Write(customer.ID + " " + customer.title + "\n");
    }

工作正常,但我似乎只能返回 1 个字段或全部字段。如何选择 p.IDp.title,而不选择其他内容?

Just experimenting with Linq:

    DataClassesDataContext db = new DataClassesDataContext();
    var q = from p in db.tblBlogEntries select p;
    foreach (var customer in q)
    {
        Response.Write(customer.ID + " " + customer.title + "\n");
    }

Works fine, but I can only seem to return 1 field, or all of them. How do I select say, p.ID and p.title, and nothing else?

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

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

发布评论

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

评论(5

烦人精 2024-10-27 21:21:24

您需要投影到匿名类型以仅返回“部分”您想要的:

 var q = from p in db.tblBlogEntries select new { p.ID, p.Title };

或者,您也可以定义一个仅具有您想要的属性子集的新类。如果您想返回投影的实例,这可能会很有用,因为匿名类型只能在本地范围内使用:

 var q = from p in db.tblBlogEntries 
         select new BlogTitleAndId() { ID = p.ID, Title = p.Title };

you need a projection to an anonymous type to only return the "parts" that you want:

 var q = from p in db.tblBlogEntries select new { p.ID, p.Title };

Alternatively you could also define a new class that only has the subset of the properties you want. This might be beneficial if you want to return instances of the projection, since anonymous types can only be used in local scope:

 var q = from p in db.tblBlogEntries 
         select new BlogTitleAndId() { ID = p.ID, Title = p.Title };
白首有我共你 2024-10-27 21:21:24

代替“select p”

使用:

var q = from p in db.tblBlogEntries select new { Column1 = p.Column1, Column2 = p.Column2 };

新的{} 生成一个内联类,因此您可以选择所需的任何列。

Instead of "select p"

Use:

var q = from p in db.tblBlogEntries select new { Column1 = p.Column1, Column2 = p.Column2 };

The new{} makes a inline class, so you can select whichever columns you need.

拥抱影子 2024-10-27 21:21:24

像这样的东西...

DataClassesDataContext db = new DataClassesDataContext();
var q = from p in db.tblBlogEntries select new { Id = p.ID, Title = p.title };
foreach (var customer in q)
{
    Response.Write(customer.ID + " " + customer.title + "\n");
}

Something like this...

DataClassesDataContext db = new DataClassesDataContext();
var q = from p in db.tblBlogEntries select new { Id = p.ID, Title = p.title };
foreach (var customer in q)
{
    Response.Write(customer.ID + " " + customer.title + "\n");
}
七度光 2024-10-27 21:21:24

var q = 从 db.tblBlogEntries 中的 p 选择新的 {p.ID, p.Title}

var q = from p in db.tblBlogEntries select new {p.ID, p.Title}

与风相奔跑 2024-10-27 21:21:24

怎么样:

DataClassesDataContext db = new DataClassesDataContext();
var q = from p in db.tblBlogEntries select new { ID = p.ID, title = p.title };
foreach (var customer in q)
{
    Response.Write(customer.ID + " " + customer.title + "\n");
}

这部分 new { ID = p.ID, title = p.title } 创建一个带有成员 IDtitle 的匿名类。您可以为任何您想要的列添加成员,类型将自动推导。不过,获得所有字段并不是什么大问题。 本文提供了更多信息和与您类似的示例。

How about this:

DataClassesDataContext db = new DataClassesDataContext();
var q = from p in db.tblBlogEntries select new { ID = p.ID, title = p.title };
foreach (var customer in q)
{
    Response.Write(customer.ID + " " + customer.title + "\n");
}

This part new { ID = p.ID, title = p.title } creates an anonymous class with members ID and title. You can add members for any column you wish and the types will be deduced automatically. Getting all the fields isn't that big a deal though. This article provides more information and a sample similar t yours.

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