简体   繁体   English

如何将LINQ查询与IQueryable结合<Guid>

[英]How can I combine a LINQ query with an IQueryable<Guid>

I have a LINQ query that uses 1 table + a large number of views. 我有一个使用1个表+大量视图的LINQ查询。 I'd like to be able to write something like this: 我希望能够编写如下内容:

IQueryable<Guid> mostViewedWriters;

switch (datePicker)
{
    case DatePicker.Last12Hours:
        mostViewedWriters = from x in context.tempMostViewed12Hours
                            select x.GuidId;
        break;
    case DatePicker.Last24Hours:
        mostViewedWriters = from x in context.tempMostViewed24Hours
                            select x.GuidId;
        break;
    case DatePicker.Last36Hours:
        mostViewedWriters = from x in context.tempMostViewed36Hours
                            select x.GuidId;
        break;
}

var query = from x1 in context.Articles
join x2 in context.Authors on x1.AuthorId == x2.AuthorId
join x3 in mostViewedWriters on x2.AuthorId == x3.Id
select new { x2.AuthorName, x1.ArticleId, x1.ArticleTitle };

The above C# is pseudo-code written to protect the innocent (me). 上面的C#是为保护无辜者(me)而编写的伪代码。 The gist of the question is this: I have a query that is related to the results of a view. 问题的要点是:我有一个与视图结果相关的查询。 That view, however, could be one of many different views. 但是,该观点可能是许多不同的观点之一。 All the views return the same data type. 所有视图都返回相同的数据类型。 I thought that I might be able to create an IQueryable that would contain the Ids that I need and use that query. 我认为我可以创建一个IQueryable,其中包含我需要的ID并使用该查询。 Alas, that effort has stalled. las,这种努力已经停滞了。

your problem is that mostViewedWriters doesn't have named fields because its just a collection/query that returns a list of Guid 's, so you should rewrite your query like this: 您的问题是mostViewedWriters没有命名字段,因为它只是一个返回Guid列表的集合/查询,因此您应该像这样重写查询:

var query = from article in context.Articles
            join author in context.Authors on article.AuthorId == author.AuthorId
            join id in mostViewedWriters on author.AuthorId == id
            select new { x2.AuthorName, x1.ArticleId, x1.ArticleTitle };

The second join condition joins directly on the value from the row, because it is just a Guid. 第二个连接条件直接连接到行中的值,因为它只是一个Guid。 (I also changed your variable names to try to make it a little clearer) (我还更改了您的变量名,以使其更清晰一些)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM