How can i convert Linq var to List
Solution 1
According to your comments you need single HyperlinInfo
object for each Attraction
value (which is string). So, use grouping and ToList()
:
private List<HyperlinkInfo> GetHyperlinkByCode()
{
TourInfoBusiness obj = new TourInfoBusiness();
List<HyperlinkInfo> lst = obj.GetAllHyperlink();
return lst.GroupBy(x => x.Attraction) // group links by attraction
.Select(g => g.First()) // select first link from each group
.ToList(); // convert result to list
}
Also you can use morelinq DistinctBy extension (available from NuGet):
private List<HyperlinkInfo> GetHyperlinkByCode()
{
TourInfoBusiness obj = new TourInfoBusiness();
List<HyperlinkInfo> lst = obj.GetAllHyperlink();
return lst.DistinctBy(x => x.Attraction).ToList();
}
Solution 2
Use Enumerable.ToList<TSource> Method
. Just Add ToList()
at the end of your query or
return k.ToList();
So your method can be:
private List<HyperlinkInfo> GetHyperlinkByCode()
{
TourInfoBusiness obj = new TourInfoBusiness();
List<HyperlinkInfo> lst = new List<HyperlinkInfo>();
lst = obj.GetAllHyperlink();
//lst = lst.Select(x => x.Attraction).ToList();
var k = lst.Select(x => x.Attraction).Distinct();
return k.ToList();
}
But x.Attraction
should be HyperLinkInfo
type object.
EDIT: Based on comment it appears that x.Attraction
is a string, you need to create object of your class Project.Bll.HyperlinkInfo
in select statement and then return that list. Something like:
var k = lst.Select(new Project.Bll.HyperLinkInfo(x => x.Attraction)).Distinct();
Assuming that Project.Bll.HyperlinkInfo
constructor takes a string parameter to return a HyperLinkInfo
object.
Solution 3
Use this:
var k = lst.Select(x => x.Attraction).Distinct().ToList();
Now k
is List of x.Attraction
type. If your x.Attraction
is string
, use this:
List<string> k = lst.Select(x => x.Attraction).Distinct().ToList();
Kartik Patel
Updated on July 10, 2022Comments
-
Kartik Patel almost 2 years
I am trying to convert the Linq var to List.my c# code is
private List<HyperlinkInfo> GetHyperlinkByCode() { TourInfoBusiness obj = new TourInfoBusiness(); List<HyperlinkInfo> lst = new List<HyperlinkInfo>(); lst = obj.GetAllHyperlink(); //lst = lst.Select(x => x.Attraction).ToList(); var k = lst.Select(x => x.Attraction).Distinct(); }
if you look at the above code till the Line var
k = lst.Select(x => x.Attraction).Distinct();
is ok Now can i convert var k to List. -
Kartik Patel about 11 yearsIts not working..The Error Message is "Error 8 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<Project.Bll.HyperlinkInfo>'
-
Kartik Patel about 11 yearsNot working....Getting an Error like Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<Project.Bll.HyperlinkInfo>'
-
Kartik Patel about 11 yearsNot working...getting an error like...Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<Project.Bll.HyperlinkInfo>'
-
Habib about 11 years@KartikPatel, check the edited part, you need to select
HyperLinkInfo
type object. -
Kartik Patel about 11 yearsI have to return object of HyperLinkInfo.I cant use List<string?
-
Kartik Patel about 11 yearsstill not working and x.Attraction is part of HyperLinkInfo type object.
-
Hamlet Hakobyan about 11 years@KartikPatel You have selected
x.Attraction
which is string. How do you want to convertstring
in toHyperLinkInfo
?