如何使用List.Distinct填充自定义类?

时间:2019-03-23 03:30:00

标签: c# mysql linq class

我有以下两个类(在C#中)

public class courseList 
{
            public string MajorName { get; set; }
            public string MajorNameID { get; set; }
            public string CourseID { get; set; }
            public string CourseName { get; set; }
}

public class CourceNames
{
        [DataMember]
        public string CourseID { get; set; }
        [DataMember]
        public string CourseName { get; set; }
}

public class Courses
{
        [DataMember]
        public string MajorNameID { get; set; }
        [DataMember]
        public string MajorName { get; set; }
        [DataMember]
        public List<CourceNames> CourseNames { get; set; }

        public Courses()
        {
            Course = new List<CourceNames>();
        }
}

我正在使用SQLreader从MYSQL数据库读取两个表

  

List<courseList> courseList

班。 我的成绩记录如下:

MajorNameID   MajorName     CourseName         CourseID
100000        Physics       Thermodynamic      PHY101
100000        Physics       Quantum            PHY200
100000        Physics       Relativity         PHY300
200000        Chemistry     Gases              CHM300
200000        Chemistry     Oreganic           CHM500
200000        Chemistry     Inroganic          CHM120
300000        Mathematics   Pure               MAT100
300000        Mathematics   Applied            MAT300 

如您所见,我想填充Courses类。我不确定如何使用Linq做到这一点。

我最近学习了以下方法,但无法正常工作。

        List<Courses> courses = courseList .GroupBy(
            d => new { d.MajorNameID , d.MajorName },
        d => d.MajorName,
        (key, g) => new courses 
        {
            MajorNameID  = key.MajorNameID,
            MajorName  = key.MajorName,
            CourseNames = g.Distinct().ToList()
        }
        ).ToList();

我收到以下错误:

> Severity  Code    Description Project File    Line    Suppression State
> Error CS0029  Cannot implicitly convert type
> 'System.Collections.Generic.List<string>' to
> 'System.Collections.Generic.List<CourceNames>'...........

我对上述内容不完全理解。有人可以帮助我如何正确设置它吗? 我想在列表中的每个专业上加载课程。

2 个答案:

答案 0 :(得分:1)

尝试通过仅按键分组并在组中的每个项目上使用<script type="text/javascript"> $('#img_' + element.event_id).append('<img src="' + '{{ url_for(' + "'static', filename=" + element.face_cap + ') }}" width="120" alt="image"'); </script> 表达式来简化组。

最后一个技巧是必须将课程名称转换为正确的类型。编译器应该给您足够的提示,但是Linq GroupBy与MySQL / TSQL GroupBy语句非常不同,因此一开始它们很难掌握。

Select

[更新:我通常不使用元素投影语法,因此我必须对其进行编译以进行检查] 使用与您的问题相同的List<Courses> courses = courseList.GroupBy( d => new { d.MajorNameID, d.MajorName } ).Select(g => new Courses { MajorNameID = g.Key.MajorNameID, MajorName = g.Key.MajorName, CourseNames = g.Distinct().Select(c => new CourceNames { CourseID = c.CourseID, CourseName = c.CourseName }).ToList() } ).ToList(); 重载(元素投影),这是相同的查询,请注意,元素选择器表达式现在选择元素,原来的帖子中有一个键表达式:

GroupBy

答案 1 :(得分:0)

lambda变量<div> <label for="img">Image</label><input type="file" name="img" id="img" value=""/> </div> <div> <img id="outImg" /> <input type="file" name="imgtbn" id="imgtbn" value=""/> </div> document.getElementById('img').onchange = function (evt) { var output = document.getElementById('outImg'); output.src = URL.createObjectURL(evt.target.files[0]); if(this.c!=undefined) { this.c.destroy(); } var el = document.getElementById('outImg'); this.c = new Croppie(el, { viewport: { width: 300, height: 300 }, boundary: { width: 900, height: 300 }, showZoomer: false, enableOrientation: true, update: function (data) { this.result('base64').then(function(dataImg) { $('#imgCropped').attr('src', dataImg); img = document.getElementById('imgCropped'); fetch(img.src) .then(res => res.blob()) .then(blob => { file = new File([blob], 'file', blob) document.getElementById('imgtbn').file = file; }); }) } }); }; 是一个组,您不能直接在其上执行g

.Distinct

您需要从List<Courses> courses = courseList .GroupBy( d => new { d.MajorNameID , d.MajorName }, d => d.MajorName, (key, g) => new Courses { MajorNameID = key.MajorNameID, MajorName = key.MajorName, CourseNames = g.Distinct().ToList() }).ToList(); 组中投影课程名称,然后在其上进行g。就像这样:

.Distinct