How to use List.Distinct to populate a custom class?How do I calculate someone's age in C#?How can I prevent SQL injection in PHP?Are static class variables possible?How do I enumerate an enum in C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I generate a random int number?Python class inherits objectWhat is a NullReferenceException, and how do I fix it?How to import an SQL file using the command line in MySQL?What does “Could not find or load main class” mean?

Is there a reason why Turkey took the Balkan territories of the Ottoman Empire, instead of Greece or another of the Balkan states?

Strange behavior editing photos in photoshop and back in lightroom

Endgame puzzle: How to avoid stalemate and win?

Given a safe domain, are subdirectories safe as well?

How to do this Padovan spiral using Mathematica?

Can I combine SELECT TOP() with the IN operator?

Can a good but unremarkable PhD student become an accomplished professor?

Determine if a grid contains another grid

Changing stroke width vertically but not horizontally in Inkscape

Where did Lovecraft write about Carcosa?

In "Avengers: Endgame", what does this name refer to?

Huffman Code in C++

Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?

How to use awk to extract data from a file based on the content of another file?

Convert Numbers To Emoji Math

Installing Debian 10, upgrade to stable later?

What is monoid homomorphism exactly?

Can an Iranian citizen enter the USA on a Dutch passport?

Efficient deletion of specific list entries

Two denim hijabs

Why didn't this character get a funeral at the end of Avengers: Endgame?

All of my Firefox add-ons been disabled suddenly, how can I re-enable them?

Which version of the Squat Nimbleness feat is correct?

How can I finally understand the confusing modal verb "мочь"?



How to use List.Distinct to populate a custom class?


How do I calculate someone's age in C#?How can I prevent SQL injection in PHP?Are static class variables possible?How do I enumerate an enum in C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I generate a random int number?Python class inherits objectWhat is a NullReferenceException, and how do I fix it?How to import an SQL file using the command line in MySQL?What does “Could not find or load main class” mean?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have the following two classes (in 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>();




I am reading two tables from MYSQL database using SQLreader to




List<courseList> courseList




class.
MY result record is as follows :



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


As u could see, I want to populate Courses class. I am not sure how I could do this using Linq.



I recently learnt the following method but it's not working correctly.



 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();


I get the following error :



> 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>'...........


I do not fully understand the above. Could someone help me how I could set it up correctly?
I want to load couses per each major in the list.










share|improve this question



















  • 1





    List<CourseNames> a = new List<string>(); - do you understand why this won't work? That's what the error message states that you're triyng to do.

    – John
    Mar 23 at 3:31












  • Yes, but how I set to coursenames ? Myclass is not atring list.

    – PCG
    Mar 23 at 3:42












  • What's your expected result? I don't think Courses collection can carry your result set from db

    – D-Shih
    Mar 23 at 3:45











  • I updated courseList class where SQLdatareader loads the data. I want to avoid repetition of MajowName and MajorNameID by using List<Courses>.

    – PCG
    Mar 23 at 4:28


















1















I have the following two classes (in 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>();




I am reading two tables from MYSQL database using SQLreader to




List<courseList> courseList




class.
MY result record is as follows :



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


As u could see, I want to populate Courses class. I am not sure how I could do this using Linq.



I recently learnt the following method but it's not working correctly.



 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();


I get the following error :



> 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>'...........


I do not fully understand the above. Could someone help me how I could set it up correctly?
I want to load couses per each major in the list.










share|improve this question



















  • 1





    List<CourseNames> a = new List<string>(); - do you understand why this won't work? That's what the error message states that you're triyng to do.

    – John
    Mar 23 at 3:31












  • Yes, but how I set to coursenames ? Myclass is not atring list.

    – PCG
    Mar 23 at 3:42












  • What's your expected result? I don't think Courses collection can carry your result set from db

    – D-Shih
    Mar 23 at 3:45











  • I updated courseList class where SQLdatareader loads the data. I want to avoid repetition of MajowName and MajorNameID by using List<Courses>.

    – PCG
    Mar 23 at 4:28














1












1








1








I have the following two classes (in 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>();




I am reading two tables from MYSQL database using SQLreader to




List<courseList> courseList




class.
MY result record is as follows :



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


As u could see, I want to populate Courses class. I am not sure how I could do this using Linq.



I recently learnt the following method but it's not working correctly.



 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();


I get the following error :



> 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>'...........


I do not fully understand the above. Could someone help me how I could set it up correctly?
I want to load couses per each major in the list.










share|improve this question
















I have the following two classes (in 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>();




I am reading two tables from MYSQL database using SQLreader to




List<courseList> courseList




class.
MY result record is as follows :



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


As u could see, I want to populate Courses class. I am not sure how I could do this using Linq.



I recently learnt the following method but it's not working correctly.



 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();


I get the following error :



> 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>'...........


I do not fully understand the above. Could someone help me how I could set it up correctly?
I want to load couses per each major in the list.







c# mysql linq class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 4:45







PCG

















asked Mar 23 at 3:29









PCGPCG

198212




198212







  • 1





    List<CourseNames> a = new List<string>(); - do you understand why this won't work? That's what the error message states that you're triyng to do.

    – John
    Mar 23 at 3:31












  • Yes, but how I set to coursenames ? Myclass is not atring list.

    – PCG
    Mar 23 at 3:42












  • What's your expected result? I don't think Courses collection can carry your result set from db

    – D-Shih
    Mar 23 at 3:45











  • I updated courseList class where SQLdatareader loads the data. I want to avoid repetition of MajowName and MajorNameID by using List<Courses>.

    – PCG
    Mar 23 at 4:28













  • 1





    List<CourseNames> a = new List<string>(); - do you understand why this won't work? That's what the error message states that you're triyng to do.

    – John
    Mar 23 at 3:31












  • Yes, but how I set to coursenames ? Myclass is not atring list.

    – PCG
    Mar 23 at 3:42












  • What's your expected result? I don't think Courses collection can carry your result set from db

    – D-Shih
    Mar 23 at 3:45











  • I updated courseList class where SQLdatareader loads the data. I want to avoid repetition of MajowName and MajorNameID by using List<Courses>.

    – PCG
    Mar 23 at 4:28








1




1





List<CourseNames> a = new List<string>(); - do you understand why this won't work? That's what the error message states that you're triyng to do.

– John
Mar 23 at 3:31






List<CourseNames> a = new List<string>(); - do you understand why this won't work? That's what the error message states that you're triyng to do.

– John
Mar 23 at 3:31














Yes, but how I set to coursenames ? Myclass is not atring list.

– PCG
Mar 23 at 3:42






Yes, but how I set to coursenames ? Myclass is not atring list.

– PCG
Mar 23 at 3:42














What's your expected result? I don't think Courses collection can carry your result set from db

– D-Shih
Mar 23 at 3:45





What's your expected result? I don't think Courses collection can carry your result set from db

– D-Shih
Mar 23 at 3:45













I updated courseList class where SQLdatareader loads the data. I want to avoid repetition of MajowName and MajorNameID by using List<Courses>.

– PCG
Mar 23 at 4:28






I updated courseList class where SQLdatareader loads the data. I want to avoid repetition of MajowName and MajorNameID by using List<Courses>.

– PCG
Mar 23 at 4:28













2 Answers
2






active

oldest

votes


















1














Try simplifying your group by to just grouping by key and using Select expression on each item in group.



This final trick is that you have to cast the course names to the correct type. The compiler should give you enough hints, but Linq GroupBy is VERY different to MySQL/TSQL GroupBy statements so they can be tricky to master at first.



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();


[UPDATE: I don't normally use element projection syntax, so I had to compile this to check]
Using the same GroupBy overload as your question (element projection), this is the same query, note that the element selector expression now selects the elements, the original post has a key expression in there instead:



List<Courses> courses2 = courseList.GroupBy(
d => new d.MajorNameID, d.MajorName , // key selector
c => new CourceNames CourseID = c.CourseID, CourseName = c.CourseName , // element selector
(key, g) => new Courses

MajorNameID = key.MajorNameID,
MajorName = key.MajorName,
CourseNames = g.Distinct().ToList()

).ToList();





share|improve this answer

























  • This works.... ! Very nice code .... I have not seen this before ...

    – PCG
    Mar 23 at 18:01


















0














The lambda variable g is a group, you cannot directly do a .Distinct on it



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();



You need to project the course name from the group g and then do a .Distinct on it. Which will be like:



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.Select(x => x.CourseName).Distinct().ToList()
).ToList();





share|improve this answer

























  • Is "(key, g) => new courses " be "(key, g) => new Courses " ? Also I can not see "x.CourseName". Is that right ?

    – PCG
    Mar 23 at 15:05












  • @PCG fixed that are you now able to access the .CourseName property ?

    – Kunal Mukherjee
    Mar 23 at 15:09











  • I will test it.

    – PCG
    Mar 23 at 18:02











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310360%2fhow-to-use-list-distinct-to-populate-a-custom-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try simplifying your group by to just grouping by key and using Select expression on each item in group.



This final trick is that you have to cast the course names to the correct type. The compiler should give you enough hints, but Linq GroupBy is VERY different to MySQL/TSQL GroupBy statements so they can be tricky to master at first.



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();


[UPDATE: I don't normally use element projection syntax, so I had to compile this to check]
Using the same GroupBy overload as your question (element projection), this is the same query, note that the element selector expression now selects the elements, the original post has a key expression in there instead:



List<Courses> courses2 = courseList.GroupBy(
d => new d.MajorNameID, d.MajorName , // key selector
c => new CourceNames CourseID = c.CourseID, CourseName = c.CourseName , // element selector
(key, g) => new Courses

MajorNameID = key.MajorNameID,
MajorName = key.MajorName,
CourseNames = g.Distinct().ToList()

).ToList();





share|improve this answer

























  • This works.... ! Very nice code .... I have not seen this before ...

    – PCG
    Mar 23 at 18:01















1














Try simplifying your group by to just grouping by key and using Select expression on each item in group.



This final trick is that you have to cast the course names to the correct type. The compiler should give you enough hints, but Linq GroupBy is VERY different to MySQL/TSQL GroupBy statements so they can be tricky to master at first.



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();


[UPDATE: I don't normally use element projection syntax, so I had to compile this to check]
Using the same GroupBy overload as your question (element projection), this is the same query, note that the element selector expression now selects the elements, the original post has a key expression in there instead:



List<Courses> courses2 = courseList.GroupBy(
d => new d.MajorNameID, d.MajorName , // key selector
c => new CourceNames CourseID = c.CourseID, CourseName = c.CourseName , // element selector
(key, g) => new Courses

MajorNameID = key.MajorNameID,
MajorName = key.MajorName,
CourseNames = g.Distinct().ToList()

).ToList();





share|improve this answer

























  • This works.... ! Very nice code .... I have not seen this before ...

    – PCG
    Mar 23 at 18:01













1












1








1







Try simplifying your group by to just grouping by key and using Select expression on each item in group.



This final trick is that you have to cast the course names to the correct type. The compiler should give you enough hints, but Linq GroupBy is VERY different to MySQL/TSQL GroupBy statements so they can be tricky to master at first.



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();


[UPDATE: I don't normally use element projection syntax, so I had to compile this to check]
Using the same GroupBy overload as your question (element projection), this is the same query, note that the element selector expression now selects the elements, the original post has a key expression in there instead:



List<Courses> courses2 = courseList.GroupBy(
d => new d.MajorNameID, d.MajorName , // key selector
c => new CourceNames CourseID = c.CourseID, CourseName = c.CourseName , // element selector
(key, g) => new Courses

MajorNameID = key.MajorNameID,
MajorName = key.MajorName,
CourseNames = g.Distinct().ToList()

).ToList();





share|improve this answer















Try simplifying your group by to just grouping by key and using Select expression on each item in group.



This final trick is that you have to cast the course names to the correct type. The compiler should give you enough hints, but Linq GroupBy is VERY different to MySQL/TSQL GroupBy statements so they can be tricky to master at first.



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();


[UPDATE: I don't normally use element projection syntax, so I had to compile this to check]
Using the same GroupBy overload as your question (element projection), this is the same query, note that the element selector expression now selects the elements, the original post has a key expression in there instead:



List<Courses> courses2 = courseList.GroupBy(
d => new d.MajorNameID, d.MajorName , // key selector
c => new CourceNames CourseID = c.CourseID, CourseName = c.CourseName , // element selector
(key, g) => new Courses

MajorNameID = key.MajorNameID,
MajorName = key.MajorName,
CourseNames = g.Distinct().ToList()

).ToList();






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 6:11

























answered Mar 23 at 6:03









Chris SchallerChris Schaller

1,4862233




1,4862233












  • This works.... ! Very nice code .... I have not seen this before ...

    – PCG
    Mar 23 at 18:01

















  • This works.... ! Very nice code .... I have not seen this before ...

    – PCG
    Mar 23 at 18:01
















This works.... ! Very nice code .... I have not seen this before ...

– PCG
Mar 23 at 18:01





This works.... ! Very nice code .... I have not seen this before ...

– PCG
Mar 23 at 18:01













0














The lambda variable g is a group, you cannot directly do a .Distinct on it



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();



You need to project the course name from the group g and then do a .Distinct on it. Which will be like:



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.Select(x => x.CourseName).Distinct().ToList()
).ToList();





share|improve this answer

























  • Is "(key, g) => new courses " be "(key, g) => new Courses " ? Also I can not see "x.CourseName". Is that right ?

    – PCG
    Mar 23 at 15:05












  • @PCG fixed that are you now able to access the .CourseName property ?

    – Kunal Mukherjee
    Mar 23 at 15:09











  • I will test it.

    – PCG
    Mar 23 at 18:02















0














The lambda variable g is a group, you cannot directly do a .Distinct on it



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();



You need to project the course name from the group g and then do a .Distinct on it. Which will be like:



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.Select(x => x.CourseName).Distinct().ToList()
).ToList();





share|improve this answer

























  • Is "(key, g) => new courses " be "(key, g) => new Courses " ? Also I can not see "x.CourseName". Is that right ?

    – PCG
    Mar 23 at 15:05












  • @PCG fixed that are you now able to access the .CourseName property ?

    – Kunal Mukherjee
    Mar 23 at 15:09











  • I will test it.

    – PCG
    Mar 23 at 18:02













0












0








0







The lambda variable g is a group, you cannot directly do a .Distinct on it



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();



You need to project the course name from the group g and then do a .Distinct on it. Which will be like:



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.Select(x => x.CourseName).Distinct().ToList()
).ToList();





share|improve this answer















The lambda variable g is a group, you cannot directly do a .Distinct on it



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();



You need to project the course name from the group g and then do a .Distinct on it. Which will be like:



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.Select(x => x.CourseName).Distinct().ToList()
).ToList();






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 15:08

























answered Mar 23 at 14:33









Kunal MukherjeeKunal Mukherjee

2,18431227




2,18431227












  • Is "(key, g) => new courses " be "(key, g) => new Courses " ? Also I can not see "x.CourseName". Is that right ?

    – PCG
    Mar 23 at 15:05












  • @PCG fixed that are you now able to access the .CourseName property ?

    – Kunal Mukherjee
    Mar 23 at 15:09











  • I will test it.

    – PCG
    Mar 23 at 18:02

















  • Is "(key, g) => new courses " be "(key, g) => new Courses " ? Also I can not see "x.CourseName". Is that right ?

    – PCG
    Mar 23 at 15:05












  • @PCG fixed that are you now able to access the .CourseName property ?

    – Kunal Mukherjee
    Mar 23 at 15:09











  • I will test it.

    – PCG
    Mar 23 at 18:02
















Is "(key, g) => new courses " be "(key, g) => new Courses " ? Also I can not see "x.CourseName". Is that right ?

– PCG
Mar 23 at 15:05






Is "(key, g) => new courses " be "(key, g) => new Courses " ? Also I can not see "x.CourseName". Is that right ?

– PCG
Mar 23 at 15:05














@PCG fixed that are you now able to access the .CourseName property ?

– Kunal Mukherjee
Mar 23 at 15:09





@PCG fixed that are you now able to access the .CourseName property ?

– Kunal Mukherjee
Mar 23 at 15:09













I will test it.

– PCG
Mar 23 at 18:02





I will test it.

– PCG
Mar 23 at 18:02

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310360%2fhow-to-use-list-distinct-to-populate-a-custom-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해