-1

I have two list(listA and listB) of following strong data type:

   public class TagPresenter
    {    
        public string TagName { get; set; }
        bool IsEnable { get; set; }     
        public string TagId { get; set; }
    }

I need to set ListA's IsEnable to true when listB's item is found in ListA. And this comparison is supposed to happend on TagId. A has all the items that B has. So in short if B has 3 item and A has 10, then in that case I need IsEnable property to true in ListA for all those 3 item. I need to do this in LINQ.Kindly help.

Chanchal
  • 47
  • 1
  • 11
  • 4
    "I need to do this in LINQ." Why? Linq is for _querying_, not _updating_. – D Stanley Jul 14 '16 at 19:28
  • @DStanley, it can also be used for updating... – GreatAndPowerfulOz Jul 14 '16 at 19:56
  • 1
    @Great.And.Powerful.Oz Really? Something like `update ListA set ListA.IsEnable = true where ...` - oops, this is SQL :) – Ivan Stoev Jul 14 '16 at 20:03
  • 2
    @Great.And.Powerful.Oz You can also use a monkey wrench to drive a nail - that doesn't mean it's the _right_ tool. – D Stanley Jul 14 '16 at 20:12
  • @DStanley: you're sounding rather blindly religious... – GreatAndPowerfulOz Jul 14 '16 at 21:41
  • 1
    @Great.And.Powerful.Oz It's not a "religious" position - there are drawbacks to using Linq for updating data. See [here](http://stackoverflow.com/questions/6386184) and [here](http://stackoverflow.com/questions/10098200) and [here](http://www.codeproject.com/Articles/762202/Csharp-Side-Effects-and-LINQs-Defferred-Execution). Yes you _can_ do it but you have to know the risks. – D Stanley Jul 14 '16 at 21:45
  • @DStanley, of course, but now you're basically saying what I said: "it can be used for updating". However, I have to admit I've always done any changing while iterating over the result of a linq query. So, strictly speaking, I've never done it in the query itself. doh! – GreatAndPowerfulOz Jul 14 '16 at 21:58

3 Answers3

3

Updating in Linq is very messy - it's more idiomatic to use Linq to find the matches but update them with a foreach

var query =
    from a in listA
    join b in listB on a.TagId equals b.TagId
    select a
    ;

foreach(var a in query)
{
    a.IsEnable = true;
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I forgot to mention that I need the output in ListA to be reflected.Will above piece of code do that? – Chanchal Jul 14 '16 at 19:33
  • 1
    What do you mean by "reflected"? If you mean the changes to be reflected when you enumerate `ListA` then yes, since the query is returning _references_ to the objects from the list. – D Stanley Jul 14 '16 at 19:41
1

This is my first post so if I make a mistake I'm sorry, and sorry for my English.

the code below is to update the properties of a list already loaded in memory using Linq:

Add Class =>

   public static class LINQToObjectExtensions
    {
        public static void UpdateAll<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (var item in source)
                action(item);
        }
    }

after:

ListA.UpdateAll(p => p.Property = Value);

or

ListA.Where(p => p.Id = 1).UpdateAll(p => p.Property = Value);
0

Try this

var query =
    from a in listA
    join b in listB on a.TagId equals b.TagId
    select new TagPresenter
    {    
       TagName = a.TagName,
       IsEnable = true,
       TagId = a.TagId
    };
neer
  • 4,031
  • 6
  • 20
  • 34