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;
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
add a comment |
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
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 thinkCoursescollection 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
add a comment |
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
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
c# mysql linq class
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 thinkCoursescollection 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
add a comment |
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 thinkCoursescollection 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
add a comment |
2 Answers
2
active
oldest
votes
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();
This works.... ! Very nice code .... I have not seen this before ...
– PCG
Mar 23 at 18:01
add a comment |
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();
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.CourseNameproperty ?
– Kunal Mukherjee
Mar 23 at 15:09
I will test it.
– PCG
Mar 23 at 18:02
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
This works.... ! Very nice code .... I have not seen this before ...
– PCG
Mar 23 at 18:01
add a comment |
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();
This works.... ! Very nice code .... I have not seen this before ...
– PCG
Mar 23 at 18:01
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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();
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.CourseNameproperty ?
– Kunal Mukherjee
Mar 23 at 15:09
I will test it.
– PCG
Mar 23 at 18:02
add a comment |
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();
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.CourseNameproperty ?
– Kunal Mukherjee
Mar 23 at 15:09
I will test it.
– PCG
Mar 23 at 18:02
add a comment |
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();
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();
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.CourseNameproperty ?
– Kunal Mukherjee
Mar 23 at 15:09
I will test it.
– PCG
Mar 23 at 18:02
add a comment |
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.CourseNameproperty ?
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
Coursescollection 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