LINQ。哪里 [英] Linq. Where

查看:59
本文介绍了LINQ。哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我创建以下Linq查询以选择与

关联的所有标签帖子:


viewData.TagsPapers =(来自数据库中的t。标签

选择新的TagPaper {

Tag = t,

Count = t.PostsTags .Count

}进tp

其中tp.Count!= 0

orderby tp.Count下降

select tp).Take(20).OrderBy(tp =>

tp.Tag.Name)。ToList();


我想选择并计算标签,但只计算与发布的帖子相关的价值。


Posts表中的Post对象有一个名为IsPublished的属性,它是

发布时为真。


如何转换我的查询以实现此目的的任何想法。


谢谢,

Miguel

Hello,

I create the following Linq query to select all the tags associated to
Posts:

viewData.TagsPapers = (from t in database.Tags
select new TagPaper {
Tag = t,
Count = t.PostsTags.Count
} into tp
where tp.Count != 0
orderby tp.Count descending
select tp).Take(20).OrderBy(tp =>
tp.Tag.Name).ToList();

I would like to select and count the tags but only the ones that are
related to Posts that are published.

Post object in Posts table has a property named IsPublished which is
true when it is published.

Any idea of how to transform my query to also accomplish this.

Thanks,
Miguel

推荐答案

如果我理解正确,你希望TagPaper.Count为计数

发布的帖子反对标签 - 是正确的吗?


在这种情况下,您应该能够使用谓词形式

Count(),假设t.PostsTags。计数实际上是

t.PostsTags.Count()[即theLINQ扩展方法,不是属性]


....

选择新的TagPaper {

Tag = t,

Count = t.PostsTags.Count(post => post.IsPublished)

}

....


Marc
If I understand correctly, you want the TagPaper.Count to be the count
of published posts against the tag - is that correct?

In which case, you should be able to use the predicate form of
Count(), assuming that t.PostsTags.Count is actually
t.PostsTags.Count() [i.e. theLINQ extension method, not a property]

....
select new TagPaper {
Tag = t,
Count = t.PostsTags.Count(post=>post.IsPublished)
}
....

Marc


9月7日,9:56 * am,Marc Gravell< marc.grav ... @ gmail.comwrote:
On Sep 7, 9:56*am, Marc Gravell <marc.grav...@gmail.comwrote:

如果我理解正确,你希望TagPaper.Count是针对标签的已发布帖子的计数

- 这是正确的吗?


在这种情况下,您应该能够使用谓词形式

Count(),假设t.PostsTags.Count实际上是

t.PostsTags.Count()[即theLINQ *扩展方法,不是属性]


...

选择新的TagPaper {

* Tag = t,

* Count = t.PostsTags.Count(post => post.IsPublished)}


...


Marc
If I understand correctly, you want the TagPaper.Count to be the count
of published posts against the tag - is that correct?

In which case, you should be able to use the predicate form of
Count(), assuming that t.PostsTags.Count is actually
t.PostsTags.Count() [i.e. theLINQ *extension method, not a property]

...
select new TagPaper {
* Tag = t,
* Count = t.PostsTags.Count(post=>post.IsPublished)}

...

Marc






是的,Count是Linq扩展方法。


但是,PostsTags是一个将帖子与标签相关联的表格,并且只有两列(PostID和TagID)




IsPublished列中的帖子表。所以我得到以下

错误:


''App.Models.PostsTag''不包含''IsPublished''的定义

且没有扩展方法''IsPublished''接受第一个参数

类型''App.Models.PostsTag''可以找到(你错过了使用

指令或汇编参考?


有什么想法吗?


谢谢,

Miguel

Hi,

Yes, Count is the Linq extension method.

However, PostsTags is a table that relates Posts with Tags and has
only two columns (PostID and TagID)

The IsPublished column in in Posts table. So I get the following
error:

''App.Models.PostsTag'' does not contain a definition for ''IsPublished''
and no extension method ''IsPublished'' accepting a first argument of
type ''App.Models.PostsTag'' could be found (are you missing a using
directive or an assembly reference?

Any idea?

Thanks,
Miguel


Posts表中的IsPublished列。所以我得到以下
The IsPublished column in in Posts table. So I get the following

错误:
error:



很抱歉,但是很难建议正确的方法,因为

我们不知道你的架构,它不是'从帖子中可以明显看出

的东西是如何联系起来的......


Marc

Sorry, but it is going to be hard to suggest the right approach since
we don''t know your schema, and it isn''t obvious from the post how
things link together...

Marc


这篇关于LINQ。哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆