C# 我如何将 Linq var 转换为 List
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15357808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i convert Linq var to List
提问by Kartik Patel
I am trying to convert the Linq var to List.my c# code is
我正在尝试将 Linq var 转换为 List.my c# 代码是
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.
如果您查看上面的代码,直到 Line vark = lst.Select(x => x.Attraction).Distinct();
正常,现在我可以将 var k 转换为 List。
采纳答案by Sergey Berezovskiy
According to your comments you need single HyperlinInfo
object for each Attraction
value (which is string). So, use grouping and ToList()
:
根据您的评论,您需要HyperlinInfo
为每个Attraction
值(即字符串)使用单个对象。所以,使用分组和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 morelinqDistinctByextension (available from NuGet):
您也可以使用morelinq DistinctBy扩展(可从 NuGet 获得):
private List<HyperlinkInfo> GetHyperlinkByCode()
{
TourInfoBusiness obj = new TourInfoBusiness();
List<HyperlinkInfo> lst = obj.GetAllHyperlink();
return lst.DistinctBy(x => x.Attraction).ToList();
}
回答by Habib
Use Enumerable.ToList<TSource> Method
. Just Add ToList()
at the end of your query or
使用Enumerable.ToList<TSource> Method
. 只需ToList()
在查询末尾添加或
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.
但x.Attraction
应该是HyperLinkInfo
类型对象。
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:
编辑:根据注释,它似乎x.Attraction
是一个字符串,您需要Project.Bll.HyperlinkInfo
在 select 语句中创建类的对象,然后返回该列表。就像是:
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.
假设Project.Bll.HyperlinkInfo
构造函数接受一个字符串参数来返回一个HyperLinkInfo
对象。
回答by Hamlet Hakobyan
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:
现在k
是x.Attraction
类型列表。如果您x.Attraction
是string
,请使用以下命令:
List<string> k = lst.Select(x => x.Attraction).Distinct().ToList();
回答by Soner G?nül
回答by Codex
Try this code:
试试这个代码:
return (List<Hyperlink Info>) k
回答by Zaheer Ahmed
try this add DistinctBy of moreLinq:
试试这个添加 DistinctBy 的moreLinq:
public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
and call it in your code:
并在您的代码中调用它:
lst.DistinctBy(x => x.Attraction).toList();