0
Guid A = Guid.Parse("8D3C940C-C802-402D-9AF2-CBAB5BAC7381");
Guid B = Guid.Parse("4961CA21-7F36-44E7-9827-002263A32F73");
Guid[] ALL = {A, B};
var result = 
    ALL.Where(x => x == Guid.Parse("4961CA21-7F36-44E7-9827-002263A32F73")).ToList()[0];
Console.WriteLine(result);

result will be

4961ca21-7f36-44e7-9827-002263a32f73

How to print name by value from array

Alex
  • 8,908
  • 28
  • 103
  • 157
  • What do you mean by 'print name by value from array'? Can you show the desired output or perhaps the desired code if that is what you mean? – Igor Mar 19 '21 at 14:35
  • By 'name' you mean 'A' or 'B'? – stuartd Mar 19 '21 at 14:35
  • result must to be B – Alex Mar 19 '21 at 14:35
  • Maybe `var result = ALL.First(x => x == B);` ? Is that what you mean? – Igor Mar 19 '21 at 14:36
  • Does this answer your question? [get name of a variable or parameter](https://stackoverflow.com/questions/9801624/get-name-of-a-variable-or-parameter) – Self Mar 19 '21 at 14:37
  • 2
    `.Where().ToList()[0]` Perhaps you just want `First()` or `FirstOrDefault()` – Self Mar 19 '21 at 14:38
  • @Self is the same, VS recomment to use [] instead of calling First – Alex Mar 19 '21 at 14:41
  • 1
    @Alex it's because of the useless ToList – Orace Mar 19 '21 at 14:43
  • @Orace yes, you are right – Alex Mar 19 '21 at 14:44
  • If you want `"A"` or `"B"`, use a dictionary and those for keys or make an class that has a `name` for a property. Once you put `A` or `B` into an array, you only have those objects and no reference or context for where they came from. – crashmstr Mar 19 '21 at 14:45
  • 1
    Bad recommendation, First is easier to read and less prone to issue. If the where clause is satisfy per multiple element. and you don't get your item wrapped into a list for no reason. – Self Mar 19 '21 at 14:45
  • FirstOrDefault() is preferred because its safer access plus you can pass it a predicate like func so you don't need where. So the where and to list are useless. FirstOrDefault() doesn't have to enumerate the list twice like where and to list will do. If you expect just one in the list use SingleOrDefault(). – Zach Hutchins Mar 19 '21 at 14:52
  • @ZachHutchins: many times I use `Single` / `SingleOrDefault` instead of `First` / `FirstOrDefault` if I'm expecting just one element. I do so to prevent logical errors. When querying from a database there may be many items matching your criteria if you use `First` you may end up with the wrong item. Of course `Single...` is less performant, but most of the time, I'm willing to live with that. – mortb Mar 19 '21 at 15:03
  • @mortb Less performant yes but it a single full enumeration is what breaks your back id consider better hardware – Zach Hutchins Mar 19 '21 at 15:10
  • @ZachHutchins I realize that you wrote in your're comment's the last sentence " If you expect just one in the list use SingleOrDefault()" :) – mortb Mar 19 '21 at 16:56
  • @Alex, I hope my answer helped you? – mortb Mar 19 '21 at 17:08
  • 1
    @mortb, yup, thx – Alex Mar 22 '21 at 08:16

1 Answers1

3

I can't think of a way to get the name of the variable B that was used to create the list. The list does not know the name of the variables used to create the list. So, for me your question is hard to answer. This is however how I would solve a problem like yours:

var list = new [] {
   new {Name = "A", Id = new Guid("8D3C940C-C802-402D-9AF2-CBAB5BAC7381"),
   new {Name = "B", Id = new Guid("4961CA21-7F36-44E7-9827-002263A32F73")}
};
var item = list.First(x => x.Id == new Guid("4961CA21-7F36-44E7-9827-002263A32F73"));
Console.WriteLine(item.Name);
mortb
  • 9,361
  • 3
  • 26
  • 44