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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:29:58  来源:igfitidea点击:

How can i convert Linq var to List

c#asp.netlinqlistvar

提问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 HyperlinInfoobject for each Attractionvalue (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.Attractionshould be HyperLinkInfotype object.

x.Attraction应该是HyperLinkInfo类型对象。

EDIT:Based on comment it appears that x.Attractionis a string, you need to create object of your class Project.Bll.HyperlinkInfoin 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.HyperlinkInfoconstructor takes a string parameter to return a HyperLinkInfoobject.

假设Project.Bll.HyperlinkInfo构造函数接受一个字符串参数来返回一个HyperLinkInfo对象。

回答by Hamlet Hakobyan

Use this:

用这个:

var k = lst.Select(x => x.Attraction).Distinct().ToList();

Now kis List of x.Attractiontype. If your x.Attractionis string, use this:

现在kx.Attraction类型列表。如果您x.Attractionstring,请使用以下命令:

List<string> k = lst.Select(x => x.Attraction).Distinct().ToList();

回答by Soner G?nül

Use ToList()to your query;

使用ToList()您的查询;

Creates a List<T>from an IEnumerable<T>.

创建一个List<T>IEnumerable<T>

List<HyperlinkInfo> k = lst.Select(x => x.Attraction).Distinct().ToList();

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