locked
LINQ - How to check values from one List to another multiple list's. RRS feed

  • Question

  • User-1024101449 posted

    Hi,

    I am already form some code. but, i want to check value and combine the result in within one single value.


    FromList value : Sub A#Sub B

    ToList1 value :

    Sub A=80

    Sub D=70

    ToList2 value :

    Sub B=100

    Sub C=90

    I want to check All the list of values FromList into ToList1 and ToList2.

    For Example,

    Sub1 value ==> is available in ToList1

    Sub2 value ==> is available in ToList2.

    Finally these two columns and values check in my table which needs to returns true/False.

    I already done the code for finding the columns which contains some coditions.

    along with this i want to add the above conditions.

    DataTable dt = new DataTable();
    dt.Columns.AddRange( new [] { new DataColumn("Sub A(MN)"), new DataColumn("Sub A(MX)"), new DataColumn("Sub B(MN)"),
    new DataColumn("Sub B(MX)") ,new DataColumn("ABS A") ,new DataColumn("ABS A1") } );
    dt.Rows.Add( "100", "100", "100", "100", "100", "100" );
    dt.Rows.Add( "200", "200", "200", "200", "200", "200" );
    dt.Rows.Add( "200", null, "200", null, "200", null );
    dt.Rows.Add( null, null, "200", null, "200", null );


    //Check whether a given column name exists or not and the Final string will be
    string str = "Sub A=100#Sub B=80";


    bool result = str.Split( '#' )
    .All( p => dt.Columns.Cast<DataColumn>().Any( c => ( c.ColumnName == p + "(MN)" || c.ColumnName == p + "(MX)" ) &&
    dt.Rows.Cast<DataRow>().Any( r => !r.IsNull( c ) && !string.IsNullOrEmpty( r[c].ToString() ) ) ) );

    The following steps to follow for the final output of true or false

    1. First check all the values of fromlist (Sub A and Sub B) in "Tolist1 and Tolist2"
    2. If valie exists then,get the list and form the string like (String str = "Sub A=80,Sub B=100")
    3. Then Check and find the corresponding columns in datatable // this is done already.
    4. Get the corresponding row for the values of Sub A=100 and Sub B = 80 which is following next step.

    5. Sub A and Sub B values range in between the rows.
    For example,
    Sub A value is 80 ==> this should check in between Sub A(MN) and Sub A(MX) &&
    Sub B value is 100 ==> this should check in between Sub B(MN) and Sub B(MX)

    5. Final result would be return true/false (sample example returns true)

    I need to alter the above query based on the above condition. where we can change.

    Friday, July 21, 2017 1:32 PM

Answers

  • User1120430333 posted

    Any help would be appreciated....

    i am getting confused while comparing list array into another array and find the columns in a table

    I mean it's kind of simple.

    You go into a foreach loop on the first list getting a value from the list to be used to find the value in the second list.

    While in the loop, you use Linq to find the value in the second list.

    var hit(from a in list2.Where(x => x.property == valuefromlist1) select a).SingleOrDefualt();

    if (hit !=null)

    {

        // you found what you were looking for

    {

    else

    {

      //otherwise you didn't find it

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 23, 2017 1:51 PM

All replies

  • User-1024101449 posted

    Any help would be appreciated....

    i am getting confused while comparing list array into another array and find the columns in a table

    Sunday, July 23, 2017 2:57 AM
  • User1120430333 posted

    Any help would be appreciated....

    i am getting confused while comparing list array into another array and find the columns in a table

    I mean it's kind of simple.

    You go into a foreach loop on the first list getting a value from the list to be used to find the value in the second list.

    While in the loop, you use Linq to find the value in the second list.

    var hit(from a in list2.Where(x => x.property == valuefromlist1) select a).SingleOrDefualt();

    if (hit !=null)

    {

        // you found what you were looking for

    {

    else

    {

      //otherwise you didn't find it

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 23, 2017 1:51 PM
  • User-1024101449 posted
    Not clear
    Sunday, July 23, 2017 3:57 PM
  • User1120430333 posted

    Not clear

    You could have looked for this yourself using Google.

    https://www.google.com/#q=find+value+in+list+from+another+list+using+linq&spf=1500833503179

    Sunday, July 23, 2017 6:14 PM
  • User-1508012637 posted

    Hi gani7787,<o:p></o:p>

    First check all the values of fromlist (Sub A and Sub B) in "Tolist1 and Tolist2"

    Could you please tell me that is fromlist a list or a string? And is ToList1 a list or a DataTable object? Please tell me more detail and post more relative code if you could for me to help you further.

    Best Regards

    Velen

    Monday, July 24, 2017 1:18 PM