Get data from IEnumerable using IEnumerable<string> as a parameter

Stepan Pavlov

I have a class of type

public class One
{
    public string firstString { get; set; }
    public string secondString { get; set; }
}

And IEnumerable:

IEnumerable<One> firstIEnumerable;

During program execution I get another IEnumerable,

IEnumerable<string> secondIEnumerable = List<string>.Where(...);

What I want is to request firstIEnumerable's secondString using equality between firstIEnumerable's firstString member and any value from secondIEnumerable.

I imagine a command like this:

IEnumerable<string> thirdIEnumerable =
    firstIEnumerable.Where(m=>m.firstString == secondIEnumerable.? ? ? ? ?).secondString; 

But it doesn't compile.

odyss-jii

If the second enumerable is a long sequence you are probably (benchmark to check) better off doing two passes and creating a hash-set, rather than doing the O(N * M) algorithm. The O(N * M) one would be:

first.Where(x => second.Contains(x.firstString)).Select(x => x.secondString)

Using a hash set:

var hashSet = new HashSet<string>(second);
first.Where(x => hashSet.Contains(x.firstString)).Select(x => x.secondString)

The second option is likely to provide much better performance in most scenarios.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using IEnumerable(Of String) to read from different kinds of data sources

From Dev

Get from IQueryable to IEnumerable

From Dev

Getting Data From IEnumerable

From Dev

Using IEnumerable<IDataRecord> to return data

From Dev

Get IEnumerable from LINQ select

From Dev

Get IEnumerable<SelectListItem> from dbContext

From Dev

How to get value from IEnumerable collection using its Key?

From Dev

Select from IEnumerable using LINQ

From Dev

C#: Get distinct T from IEnumerable<IEnumerable<T>>

From Dev

Convert IEnumerable<string> into IEnumerable<FileInfo>

From Dev

Remove sub IEnumerable from IEnumerable

From Dev

IEnumerable<string> checkboxes get old date

From Dev

Get value from IEnumerable foreach loop

From Dev

How to get's the model metadata from an IEnumerable?

From Dev

How to get's the model metadata from an IEnumerable?

From Dev

Transferring data from controller to view - List<> / IEnumerable<>?

From Dev

IEnumerable as Out Parameter

From Dev

Add items to list from IEnumerable using LinQ

From Dev

How do I get the type of a generic parameter on IEnumerable<object>?

From Dev

How can I cast IEnumerable<?> to IEnumerable<string>?

From Dev

Filter IEnumerable<T> against IEnumerable<string>

From Dev

Filter IEnumerable<T> against IEnumerable<string>

From Dev

Cast IEnumerable<string> to IEnumerable<int> in C#

From Dev

How to convert IEnumerable<char> to IEnumerable<string>?

From Dev

Convert lambda type from int => IEnumerable<string>> to int => Task<IEnumerable<string>>>?

From Dev

How to cast IEnumerable to IEnumerable<T> with dynamic data

From Dev

IEnumerable<string> to Stream for FileStreamResult

From Dev

Casting string to IEnumerable<object>

From Dev

Group IEnumerable into a string

Related Related

  1. 1

    Using IEnumerable(Of String) to read from different kinds of data sources

  2. 2

    Get from IQueryable to IEnumerable

  3. 3

    Getting Data From IEnumerable

  4. 4

    Using IEnumerable<IDataRecord> to return data

  5. 5

    Get IEnumerable from LINQ select

  6. 6

    Get IEnumerable<SelectListItem> from dbContext

  7. 7

    How to get value from IEnumerable collection using its Key?

  8. 8

    Select from IEnumerable using LINQ

  9. 9

    C#: Get distinct T from IEnumerable<IEnumerable<T>>

  10. 10

    Convert IEnumerable<string> into IEnumerable<FileInfo>

  11. 11

    Remove sub IEnumerable from IEnumerable

  12. 12

    IEnumerable<string> checkboxes get old date

  13. 13

    Get value from IEnumerable foreach loop

  14. 14

    How to get's the model metadata from an IEnumerable?

  15. 15

    How to get's the model metadata from an IEnumerable?

  16. 16

    Transferring data from controller to view - List<> / IEnumerable<>?

  17. 17

    IEnumerable as Out Parameter

  18. 18

    Add items to list from IEnumerable using LinQ

  19. 19

    How do I get the type of a generic parameter on IEnumerable<object>?

  20. 20

    How can I cast IEnumerable<?> to IEnumerable<string>?

  21. 21

    Filter IEnumerable<T> against IEnumerable<string>

  22. 22

    Filter IEnumerable<T> against IEnumerable<string>

  23. 23

    Cast IEnumerable<string> to IEnumerable<int> in C#

  24. 24

    How to convert IEnumerable<char> to IEnumerable<string>?

  25. 25

    Convert lambda type from int => IEnumerable<string>> to int => Task<IEnumerable<string>>>?

  26. 26

    How to cast IEnumerable to IEnumerable<T> with dynamic data

  27. 27

    IEnumerable<string> to Stream for FileStreamResult

  28. 28

    Casting string to IEnumerable<object>

  29. 29

    Group IEnumerable into a string

HotTag

Archive