条件 var 推断类型查询 LINQ to XML 的替代编码?

发布于 2024-11-03 21:17:35 字数 1582 浏览 3 评论 0原文

这是此处找到的相关主题的后续内容 https://stackoverflow.com/questions/1987485/conditionally- 则分配-c-var-as-elegant-as-it-gets

如果我执行以下操作,

  var query = (SearchString == "" ?
    (
         from MEDIA in xdoc.Descendants("MEDIA")
                    select new
                    {
                        PLAY = MEDIA.Element("PLAY").Value,
                        PIC = MEDIA.Element("PIC").Value,
                        TTL = MEDIA.Element("TTL").Value
                    }
    ):


        from MEDIA in xdoc.Descendants("MEDIA")
                    where MEDIA.Element("TTL").ToString().ToLower().Contains(SearchString)
                    select new
                    {
                        PLAY = MEDIA.Element("PLAY").Value,
                        PIC = MEDIA.Element("PIC").Value,
                        TTL = MEDIA.Element("TTL").Value
                    }
    ) ;

:如何声明查询类型以使其在类级别静态?

或者,在引用的帖子 Marc Gravell 中指出了一种不同的方法:

IQueryable<Part> query = db.Participant;
if(email != null) query = query.Where(p => p.EmailAddress == email);
if(seqNr != null) query = query.Where(p => p.SequenceNumber == seqNr);
...

在我的情况下,我将如何声明/重新编码查询? 这是我的疯狂尝试:)

   IEnumerable<XElement> query = xdoc.Descendants("MEDIA");
    if (SearchString != "" )
        query = query.Where(m => m.Element("TTL").ToString().ToLower().Contains(SearchString));

谢谢。

This is a follow up on a related topic found here
https://stackoverflow.com/questions/1987485/conditionally-assign-c-var-as-elegant-as-it-gets

if I am doing the following:

  var query = (SearchString == "" ?
    (
         from MEDIA in xdoc.Descendants("MEDIA")
                    select new
                    {
                        PLAY = MEDIA.Element("PLAY").Value,
                        PIC = MEDIA.Element("PIC").Value,
                        TTL = MEDIA.Element("TTL").Value
                    }
    ):


        from MEDIA in xdoc.Descendants("MEDIA")
                    where MEDIA.Element("TTL").ToString().ToLower().Contains(SearchString)
                    select new
                    {
                        PLAY = MEDIA.Element("PLAY").Value,
                        PIC = MEDIA.Element("PIC").Value,
                        TTL = MEDIA.Element("TTL").Value
                    }
    ) ;

How would I declare the query type to make it static at the class level?

Alternatively, in the referenced post Marc Gravell point out a different approach

IQueryable<Part> query = db.Participant;
if(email != null) query = query.Where(p => p.EmailAddress == email);
if(seqNr != null) query = query.Where(p => p.SequenceNumber == seqNr);
...

How would I declare/recode the query in my case?
Here is my wild attempts :)

   IEnumerable<XElement> query = xdoc.Descendants("MEDIA");
    if (SearchString != "" )
        query = query.Where(m => m.Element("TTL").ToString().ToLower().Contains(SearchString));

Thank you.

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

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

发布评论

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

评论(1

终遇你 2024-11-10 21:17:35

如何声明查询类型以使其在类级别静态?

你不能。匿名类型是匿名的......所以它们没有可用于声明变量的名称。您的查询的类型为 IEnumerable,但您无法在代码中引用 something。因此,您需要创建一个表示查询结果的特定类,并使用它而不是匿名类型。

How would I declare the query type to make it static at the class level?

You can't. Anonymous types are, well, anonymous... so they don't have a name you can use to declare variables. Your query is of type IEnumerable<something>, but you can't refer to something in your code. So you need to create a specific class that represent the results of your query, and use it instead of the anonymous type.

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