LinQ查询不支持的LinQ查询和两阶段查询

时间:2013-02-12 11:49:25

标签: c# linq sharepoint sharepoint-2010

我想从Assessment表中获取moduleName的不同值。 ModuleName是Assessment-List中的查阅列。 ModuleName从ModuleList链接。请告诉我如何从评估列表中获得不同的模块名称?

protected void drpCoursesName_SelectedIndexChanged(object sender, EventArgs e)
{
    using (SiteEntitiesDataContext siteEntitiesDataContext = new SiteEntitiesDataContext(SPContext.Current.Site.Url))
    {
        EntityList<AssessmentItem> listItems = siteEntitiesDataContext.GetList<AssessmentItem>("Assessment");
        var distinctModuleCount = from module1 in listItems
                                  group module1 by new { module1.ModuleName }
                                  into grp
                                  select new
                                  {
                                      count1=grp.Count(),
                                  };
        int modulecount=distinctModuleCount.Count();

        foreach(var item in distinctModuleCount)
        {
            Label lbl1 = new Label();
            lbl1.Text = modulecount.ToString();
            PlaceHolderAssessment.Controls.Add(lbl1);
        }//end of for loop
    }//end of Data Context
}//end of method

提前致谢。

1 个答案:

答案 0 :(得分:0)

要准确解释你的目标有点困难,但我希望以下有所帮助。

给定一个定义为:

的类
public class AssessmentItem
{
    public string ModuleName { get; set; }
}

以下代码初始化并查询这些类的集合:

var items = new List<AssessmentItem>
    {
        new AssessmentItem { ModuleName = "Test 1" },
        new AssessmentItem { ModuleName = "Test 1" },
        new AssessmentItem { ModuleName = "Test 2" },
        new AssessmentItem { ModuleName = "Test 2" },
        new AssessmentItem { ModuleName = "Test 2" }
    };

var grouped = items.GroupBy(x => x.ModuleName);
Debug.WriteLine("Distinct modules {0}", grouped.Count());
foreach (var group in grouped)
{
    Debug.WriteLine("{0} has {1} entries", group.Key, group.Count());
}

您将收到以下输出:

Distinct modules 2
Test 1 has 2 entries
Test 2 has 3 entries