Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架4.1统计相同的数据库行 我的设置_C#_Asp.net Mvc 3_Entity Framework_Entity Relationship - Fatal编程技术网

C# 实体框架4.1统计相同的数据库行 我的设置

C# 实体框架4.1统计相同的数据库行 我的设置,c#,asp.net-mvc-3,entity-framework,entity-relationship,C#,Asp.net Mvc 3,Entity Framework,Entity Relationship,我有两个类,这里显示的是本例所需的内容: public class Photo : Entity { [Key] public int Id { get; set; } public virtual ICollection<Tag> Tags { get; set; } public Photo() { Tags = new List<Tag>(); } } public class Tag : En

我有两个类,这里显示的是本例所需的内容:

public class Photo : Entity
{
    [Key]
    public int Id { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }

    public Photo()
    {
        Tags = new List<Tag>();
    }
}


public class Tag : Entity
{
    [Key]
    public string Text { get; set; }

    public virtual ICollection<Photo> Photos { get; set; }

    public Tag()
    {
        Photos = new List<Photo>();
    }
}
EF/LINQ等效值是多少

第二个问题
如何获取每个标签的计数或50个最常用标签的计数?

按标签计数:

from t in Context.Tags
select new
{
    t.Text,
    t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
    Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}
最常用的标签:

from t in Context.Tags
select new
{
    t.Text,
    t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
    Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}
50个最常用的标签(实际上可能没有50个):

from t in Context.Tags
select new
{
    t.Text,
    t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
    Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}
写意,所以可能不是100%语法正确,也不是最小/最可读的方式

编辑:添加示例:

from t in Context.Tags
select new
{
    t.Text,
    t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
    t.Text,
    photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
    Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}

谢谢,看起来不错。你能用一个例子来进一步说明如何循环所有找到的标记及其计数吗?非常好!有没有更简单的方法只获取最低的标签计数和最高的标签计数?您只需要数字吗?或者你想要数字和标签文本吗?算出了:
intmincount=result.Min(x=>x.Count)