C# 从 linq 查询获取列表中的第一项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16451339/
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 01:02:10  来源:igfitidea点击:

Get first item in list from linq query

c#linq

提问by

I wish to get just the very first record I realize the the "Take() will not work.

我希望获得我意识到“Take() 不起作用的第一条记录”。

So I have a List that queries another list

所以我有一个查询另一个列表的列表

List<string> etchList = new List<string>();
etchList.Add("709");

The Linq query is

Linq 查询是

var query = (from vio in AddPlas
            where etchList.Any(vioID => vio.Key.Formatted.Equals(vioID))
            select new
            {
                EtchVectors = vio.Shapes
            }).ToList().Take(1);

Now this "Take(1)" will only work if I had other data in etchList, so it is not what I am looking for. The results I'm getting look like this:

现在这个“Take(1)”只有在我在 etchList 中有其他数据时才有效,所以这不是我要找的。我得到的结果是这样的:

Formatted
"clinesegs" 1013.98 5142.96 "LYR3_SIG2"
"clinesegs" 1020.16 5168.33 "LYR3_SIG2"
"clinesegs" 967.03 5151.31 "LYR3_SIG2"
"clinesegs" 971.43 5174.01 "LYR3_SIG2"

格式化的
“clinesegs” 1013.98 5142.96 “LYR3_SIG2”
“clinesegs” 1020.16 5168.33 “LYR3_SIG2”
“clinesegs” 967.03 5151.31 “LYR3_SIG2”
“clinesegs” 9717R3_SIG3_SIG3

I wish to ONLY get back the first "row in the list of

我希望只取回列表中的第一行

"clinesegs" 1013.98 5142.96 "LYR3_SIG2"

EDIT:

编辑:

Ok this code exist:

好的,此代码存在:

public class AddPla
{
public AddPla()
{
    Shapes = new List<Parse>();
}

public Parse Key { get; set; }
public Parse Pla { get; set; }
public Parse Angle { get; set; }
public Parse Default { get; set; }
public List<Parse> Shapes { get; set; }
public Parse DoubleBrace { get; set; }
public Parse Number1 { get; set; }
public Parse Number2 { get; set; }
  }

  public class Parse
  {

public string Formatted { get; set; }
public string Original { get; set; }

  }

Then this List

那么这个列表

  var AddPlas = new List<AddPla>();

采纳答案by gunr2171

I don't suppose it's as easy as First()?

我不认为它像First()?

If you expect that your query will not return any results, use FirstOrDefault()

如果您希望查询不会返回任何结果,请使用 FirstOrDefault()

Update

更新

If what you are asking for is "Get the first Shapesfrom each AddPla", then add the Firstto your selectstatement, rather than at the end

如果您要求的是“Shapes从每个人中获得第一个AddPla”,则将其添加First到您的select语句中,而不是在最后

select new { EtchVectors = vio.Shapes.First() }

回答by Sachin

You can try with FirstOrDefault.

您可以尝试使用FirstOrDefault

var query = (from vio in AddPlas
            where etchList.Any(vioID => vio.Key.Formatted.Equals(vioID))
            select new
            {
                EtchVectors = vio.Shapes
            }).FirstOrDefault();

回答by p.s.w.g

Use the FistOrDefaultmethod to safely return the first item from your query, or nullif the query returned no results:

使用该FistOrDefault方法安全地返回查询中的第一项,或者null如果查询未返回任何结果:

var result =
    (from vio in AddPlas
     where etchList.Any(vioID => vio.Key.Formatted.Equals(vioID))
     select new
     {
        EtchVectors = vio.Shapes.FistOrDefault()
     })

Or equivalently:

或等效地:

var result = AddPlas.Where(x => etchList.Any(y => x.Key.Formatted.Equals(y)))
                    .Select(x => new { EtchVectors = x.Shapes.FistOrDefault() });