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. I'd like to be able to write something lik开发者_开发百科e 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). 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. Alas, that effort has stalled.
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:
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. (I also changed your variable names to try to make it a little clearer)
精彩评论