C# LINQ 错误帮助

标签 c# .net linq linq-to-sql

下面是 LINQ 语句的摘录,该语句抛出以下错误。有没有办法像我在下面做的那样引用 ID (ID = af.AgileFactorID)?....psf.AgileFactorID == tagSummary.ID ?预先感谢您的帮助!

select new {
    ID = af.AgileFactorID,
    Total = psf.Count()
};

1) 当前上下文中不存在名称 af,2) 当前上下文中不存在名称 psf

var tagCloud = from psf in tagSummary where psf.AgileFactorID == tagSummary.ID

1) 错误 62“AnonymousType#1”不包含“AgileFactorID”的定义,并且找不到接受“AnonymousType#1”类型的第一个参数的扩展方法“AgileFactorID”(您是否缺少 using 指令或汇编引用?)

2) 错误 63“System.Collections.Generic.IEnumerable”不包含“ID”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“ID” (您是否缺少 using 指令或程序集引用?)

这是完整的查询:

private void BindTagCloud()
{



var tagSummary = from af in db.AgileFactors
           join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
           join s in db.Stories on psf.StoryID equals psf.StoryID
           join pim in db.ProjectIterationMembers on pim.ProjectIterationMemberID equals s.ProjectIterationMemberID
           join i db.Iteration on ...
           join p db.Project on ....
           where p.ProjectID == proj_id &&
                 p.ProjectID == i.ProjectID &&
                 i.ProjectIterationID == pim.ProjectIterationID &&
                 pim.ProjectIterationMemberID == s.ProjectIterationMemberID &&
                 s.StoryID == psf.StoryID &&
                 psf.AgileFactorID == af.AgileFactorID
                 group af by af.Name into tagGroup

                 select new
                 {

                    ID = af.AgileFactorID,
                    Total = psf.Count() 

                 };


 var tagCloud = from psf in tagSummary
         where psf.AgileFactorID == tagSummary.ID
 select new
 {

 Name = psf.Name,
 ID = psf.AgileFactionID,
 Count = psf.Count(),

 weight = Count / tagSummary.Total * 100

};


ListView1.DataSource = tagCloud; 
ListView1.DataBind();

}

最佳答案

您的“完整查询”可能不正确,因为它有许多语法错误和缺失部分。

不过,我能重构的最好的是:

var tagSummary =
    from af in db.AgileFactors
    join psf in db.ProjectStoryFactors
        on af.AgileFactorID equals psf.AgileFactorID
    join s in db.Stories on psf.StoryID equals s.StoryID
    join pim in db.ProjectIterationMembers
        on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
    join i in db.Iteration
        on pim.ProjectIterationID equals i.ProjectIterationID
    join p in db.Project on i.ProjectID equals p.ProjectID
    where p.ProjectID == proj_id
    group af by new { af.Name, af.AgileFactorID } into tagGroup
    select new
    {
        ID = tagGroup.Key.AgileFactorID,
        Name = tagGroup.Key.Name,
        Count = tagGroup.Count()
    };


var tagCloud =
    from t in tagSummary
    join psf in db.ProjectStoryFactors
        on t.ID equals psf.AgileFactorID into psfs
    let Count = psfs.Count()
    let Total = tagSummary.Count()
    select new
    {
        t.Name,
        t.ID,
        Count,
        Weight = (double)Count / Total * 100,
    };

这个查询如何为您工作?很近吗?

关于C# LINQ 错误帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4785963/

相关文章:

C# 为什么 127 = 这个位串?

C# - 在此示例中改进属性封装?

c# - 创建类似字符串的 String.Format 并提取子字符串

c# - WebRequest 使用 Mozilla Firefox

c# - 使用 linq 检索平均分数

c# - 在 3 个逗号后添加换行符

c# - C#中调用方法语法时赋值

c# - vtables是如何在c++和c#中实现的?

.NET 6 使用旧的 WCF 依赖项引用 .Net 4.8

c# - IEnumerable.Except() 和自定义比较器