locked
How to return the name of a field and the record in the same linq query RRS feed

  • Question

  • User-140672246 posted

    How should I create an efficient search of a code on a table, where the code to be found can be in any field?

    I need to find in a search usanco Linq, two results:

    1) Find a certain value in various fields in the same table?
    2) also return the name of the field where the value found?

    example: find the code 20 in the following table fields: field 1 or field 2 or field 3 or field 4

    table

    Field 1    Field 2    Field 3   Field 4       ID
    =====   =====   =====  =====    =====

        0            0            20          2               1
        1            4             8           0               2

    Result to return:

             name found field => "Field 3"
             Found ID =>   1

    Tuesday, June 9, 2015 3:41 PM

Answers

  • User-84896714 posted

    Hi Link_
    Thank you for your post.
    It's hard to use linq to search for this scene.
    I wrote demo use normal loop for your reference.

    public class Result
    {
        public string ColumnName { get; set; }
    
        public int ID { get; set; }
    }
    public class Table
    {
        public int Field1 { get; set; }
        public int Field2 { get; set; }
        public int Field3 { get; set; }
        public int Field4 { get; set; }
        public int ID { get; set; }
    }
    public List<Result> Search(List<Table> datas, int value)
    {
        List<Result> list = new List<Result>();
        foreach (var item in datas)
        {
            string column = "";
            if (item.Field1 == value)
            {
                column = "Field1";
            }
            else if (item.Field2 == value)
            {
                column = "Field2";
            }
            else if (item.Field3 == value)
            {
                column = "Field3";
            }
            else if (item.Field4 == value)
            {
                column = "Field4";
            }
            if (column != "")
            {
                list.Add(new Result { ColumnName = column, ID = item.ID });
            }
        }
        return list;
    }

    Hoping my reply could be helpful to you.
    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2015 5:03 AM

All replies

  • User-84896714 posted

    Hi Link_
    Thank you for your post.
    It's hard to use linq to search for this scene.
    I wrote demo use normal loop for your reference.

    public class Result
    {
        public string ColumnName { get; set; }
    
        public int ID { get; set; }
    }
    public class Table
    {
        public int Field1 { get; set; }
        public int Field2 { get; set; }
        public int Field3 { get; set; }
        public int Field4 { get; set; }
        public int ID { get; set; }
    }
    public List<Result> Search(List<Table> datas, int value)
    {
        List<Result> list = new List<Result>();
        foreach (var item in datas)
        {
            string column = "";
            if (item.Field1 == value)
            {
                column = "Field1";
            }
            else if (item.Field2 == value)
            {
                column = "Field2";
            }
            else if (item.Field3 == value)
            {
                column = "Field3";
            }
            else if (item.Field4 == value)
            {
                column = "Field4";
            }
            if (column != "")
            {
                list.Add(new Result { ColumnName = column, ID = item.ID });
            }
        }
        return list;
    }

    Hoping my reply could be helpful to you.
    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2015 5:03 AM
  • User-140672246 posted

    Good morning Wang Li,

        Thank you for being willing to help me.

        Your code was well organized and clean. I thought about using "Reflection" for this, but unfortunately I do not I hold enough knowledge to perform this task.

        Actually work with Linq to this goal is not easy, but your code will serve my purposes.

        Thank Li

    Wednesday, June 10, 2015 10:04 AM