Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How to update data when it is appears in more than one section by LINQ

$
0
0

see the screen shot 

see LineItem R&D and XfundCode RD appears in consensus and also in key drug section. i want if any specific combination of LineItem & XfundCode appears in multiple section then xfundcode will be empty from the second occurrence.

xfundcode is attached with line item. it is a property of line item.

so output will be....screen shot attached 

i tried this code but did not get desire output.

namespace LINQ_Remove_Duplicate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            List<Data> _lstMainData = new List<Data>
            {
                new Data() { Section="Consensus", Lineitem = "R&D", xfundcode = "RD" },
                new Data() { Section="Consensus",Lineitem = "SG&A", xfundcode = "SGA" },
                new Data() { Section="Key Drugs",Lineitem = "R&D", xfundcode = "RD" },
                new Data() { Section="Key Drugs",Lineitem = "SG&A", xfundcode = "SGA" }
            };

            var groupResults = _lstMainData
                .GroupBy(data => new
                {
                    LineItem = data.Lineitem,
                    xFundcode = data.xfundcode
                })
                .Select(g => new
                {
                    Count = g.Count(),
                    LineItem = g.Key.LineItem,
                    xfundcode = g.Key.xFundcode,
                    List = g.ToList()

                })
                .Where(dataGroup => dataGroup.Count > 1)
                .ToList();

            for (int index = 0; index < groupResults.Count; index++)
            {
                for (int listIndex = 1; listIndex < groupResults[index].List.Count; listIndex++)
                {
                    groupResults[index].List[listIndex].xfundcode = "";
                }
            }
        }
    }

    public class Data
    {
        public string Section { get; set; }
        public string Lineitem { get; set; }
        public string xfundcode { get; set; }
    }
}

please share the right code sample which will find & update xfundcode to empty value if any combination of line item and xfund code appear in more than one section.

thanks


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>