Share via


LINQ - How to search list values from another multiple list.

Question

Monday, July 24, 2017 5:27 AM

Hi,

How to find and search list values which is available or not from multiple another lists.

For example,

 List<string> FromList = new List<string>() { "Sub A", "Sub B" };
        List<string> List1 = new List<string>() { "Sub A=80", "Sub D=70" };
        List<string> List2 = new List<string>() { "Sub B=100", "Sub C=90" };

I want to take Fromlist and check this will be available or not in List1 and List2.

if it is available then it returns result as Sub A=80 and Sub B=100.

if it is not available in both list (List1 and List2) then it returns false.

How to form in LINQ Query..?

All replies (5)

Tuesday, July 25, 2017 9:30 AM âś…Answered

Hi gani7787,

Do you mean your final result is a string?

If so , please try to use following code:

            List<string> FromList = new List<string>() { "Sub A", "Sub B" };
            List<string> List1 = new List<string>() { "Sub A=80", "Sub D=70" };
            List<string> List2 = new List<string>() { "Sub B=100", "Sub C=90" };
            List<string> result = new List<string>();
            foreach (var sub in FromList)
            {
                result.AddRange(List1.Where(x => x.Contains(sub)).ToList());
                result.AddRange(List2.Where(x => x.Contains(sub)).ToList());
            }
            if (result.Count != 0)
            {
                string re = "result=";
                foreach (var r in result)
                {
                    re = re + r + ",";
                }
                re = re.Remove(re.Length - 1);
                return re;

            }else
            {
                return "false";
            }

Best Regards,

Billy


Monday, July 24, 2017 8:55 AM

Hi gani7787,

Please try to use following code:

            List<string> FromList = new List<string>() { "Sub A", "Sub B" };
            List<string> List1 = new List<string>() { "Sub A=80", "Sub D=70" };
            List<string> List2 = new List<string>() { "Sub B=100", "Sub C=90" };
            List<string> result = new List<string>();
            foreach (var sub in FromList)
            {
                result.AddRange(List1.Where(x => x.Contains(sub)).ToList());
                result.AddRange(List2.Where(x => x.Contains(sub)).ToList());
            }
            if (result.Count == 0)
            {
                return false;
            }

Best regards,

Billy


Tuesday, July 25, 2017 8:56 AM

At the final result i want to form below format.

result = Sub A=80,Sub B=100

How to change the code..?


Tuesday, July 25, 2017 11:03 AM

Thanks for your help.

I am checking some condition using the below conditions.

For the sample example, condition will not match. so It should return False.

But, It returns "True"..How....?

 protected void Button6_Click(object sender, EventArgs e)
    {
        List<string> FromList = new List<string>() { "Sub A", "Sub B" };
        List<string> List1 = new List<string>() { "Sub A=80", "Sub D=70" };
        List<string> List2 = new List<string>() { "Sub B=100", "Sub C=90" };

       
        List<string> result = new List<string>();
        string str = null;
        foreach (var sub in FromList)
        {

            result.AddRange(List1.Where(x => x.Contains(sub)).ToList());
            result.AddRange(List2.Where(x => x.Contains(sub)).ToList());

        }
        if (result.Count != 0)
        {
            
            foreach (var r in result)
            {
                str = str + r + ",";
            }
            str = str.Remove(str.Length - 1);
            //return "true";

        }
        else
        {
            //return "false";
        }

        //List<string> str = new List<string>() { "Sub A=80", "Sub B=100" }; // Range should check between range of minimum and maximum of both subjects
        string str1 = str.ToString();
        string DEPT = "S6";
        double Dist = 30.0;
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new[] { new DataColumn("TYPE"),new DataColumn("Sub A(MN)"), new DataColumn("Sub A(MX)"), new DataColumn("Sub B(MN)"),
new DataColumn("Sub B(MX)") ,new DataColumn("Sub A") ,new DataColumn("Sub A1"),new DataColumn("FROM"),new DataColumn("TO"),new DataColumn("DEPT") });
        dt.Rows.Add(null, "185", "100", "70", "100", null, null, 20, 100, "S6");
        dt.Rows.Add(null, null, null, "200", "200", "200", "200", 35, 100, "S7");
        dt.Rows.Add(null, null, null, "100", "100", "100", "100", 35, 100, "S7");
        dt.Rows.Add(null, null, "400", null, "100", "100", "100", 35, 100, "S7");
        dt.Rows.Add(null, null, null, "100", null, null, "100", 35, 100, "S7");
        dt.Rows.Add(null, null, "700", null, "100", null, "100", 35, 100, "S7");
        dt.Rows.Add(null, null, null, "100", null, "100", "100", 35, 100, "S7");


        int place = 0;

        var results = (from row in dt.AsEnumerable()
                               from pair in
                                   (from term in str1.Split (new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                    let pair = term.Split(new[] { '=' })
                                    where pair.Count() == 2 && int.TryParse(pair[1], out place)
                                    select new KeyValuePair<string, int>(pair[0], place))
                               where row[pair.Key + "(MN)"] != DBNull.Value && row[pair.Key + "(MX)"] != DBNull.Value 
                               
                               
                               let r1 = Convert.ToInt64(row[pair.Key + "(MN)"])
                               let r2 = Convert.ToInt64(row[pair.Key + "(MX)"])

                               
                               let FrDist = Convert.ToInt64(row.Field<string>("FROM"))
                               let ToDist = Convert.ToInt64(row.Field<string>("TO"))
                               where pair.Value >= r1 && pair.Value <= r2 &&
                               !string.IsNullOrWhiteSpace(row.Field<string>("DEPT")) && row.Field<String>("DEPT") == DEPT &&
                               !string.IsNullOrEmpty(row.Field<string>("FROM")) && !string.IsNullOrEmpty(row.Field<string>("TO")) &&
                               Dist >= FrDist && Dist <= ToDist
                               select row).Any();

        bool res = results; 
         

    }

Wednesday, July 26, 2017 2:41 AM

Hi gani7787,

It seems that the problem in your original posting has been solved.

Please create a new thread for your new question.

Thanks for your cooperation.

Best Regards,

Billy