I have three classes A, B, Common.
public class A
{
public int Id { get; set; }
public string Name { get; set; }
}
public class B
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Common
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
I want to put all A and B into a collection of Common
var result = new List<Common>();
foreach (var i in A_collection) {
result.Add(new Common() {
Id = i.Id,
Name = i.Name,
Type = "This is A"
});
}
foreach (var i in B_collection) {
result.Add(new Common() {
Id = i.Id,
Name = i.Name,
Type = "This is B"
});
}
Please notice that I cannot modify any code in class A, B and Common. But I can add new classes
Is there a way to use generics to simplify the code to just use one loop like this?
foreach (var i in T_collection) {
result.Add(new Common() {
Id = i.Id,
Name = i.Name,
Type = "This is T"
});
}
If your use case allows you to use interface in the places of the objects, and A and B are not sealed class, then you can wrap A and B in your own class with an interface. Something like this:
public interface ICommon
{
int Id { get; set; }
string Name { get; set; }
}
public class MyA : A, ICommon {}
public class MyB : B, ICommon {}
then instead of creating new instances of A or B, you can create instances of MyA and MyB, which will allow you to work with ICommon while still being interchangeable for A or B, or wherever you declare objects of A or B, just replace them with MyA or MyB.
Use LINQ?
var lA = new List<A>();
var lB = new List<B>();
var a = lA.Select(a => new Common(){ Id = a.Id, Name = a.Name, Type = "This is A"});
var b = lB.Select(b => new Common(){ Id = b.Id, Name = b.Name, Type = "This is B"});
var all = a.Append(b).ToList();
Not impossible in technical terms. First you write methods to create Common from object...
public static Common GenerateCommon(object o)
{
if (o is A)
{
return GenerateCommon(o as A);
}
if (o is B)
{
return GenerateCommon(o as B);
}
throw new Exception("Object is neither A nor B");
}
public static Common GenerateCommon(A a)
{
return new Common()
{
Id = a.Id,
Name = a.Name,
Type = "This is A"
};
}
public static Common GenerateCommon(B b)
{
return new Common()
{
Id = b.Id,
Name = b.Name,
Type = "This is B"
};
}
Then concatenate two lists as list of objects.
List<A> alist = new List<A>();
alist.Add(new A() { Id = 1, Name = "asd" });
alist.Add(new A() { Id = 2, Name = "2dd" });
alist.Add(new A() { Id = 3, Name = "3q" });
List<B> blist = new List<B>();
blist.Add(new B() { Id = 4, Name = "4asd" });
blist.Add(new B() { Id = 5, Name = "5dd" });
blist.Add(new B() { Id = 6, Name = "63q" });
List<object> olist = alist.Select(x => (object)x).ToList();
olist.AddRange(blist.Select(x => (object)x));
List<Common> clist = new List<Common>();
foreach(var o in olist)
{
clist.Add(GenerateCommon(o));
}
But I think modifying A/B/Common or following #T.S.'s answer is better.
I think I'd just write an extension method pair:
public static Common AsCommon(this A a)
{
return new Common()
{
Id = a.Id,
Name = a.Name,
Type = "This is A"
};
}
public static Common AsCommon(this B b)
{
return new Common()
{
Id = b.Id,
Name = b.Name,
Type = "This is B"
};
}
And then e.g.:
listOfCommon.AddRange(listOfA.Select(AsCommon));
Or e.g.:
listOfCommon = listOfA.Select(AsCommon).Concat(listOfB.Select(AsCommon)).ToList();
Probably also worth pointing out that using AutoMapper can save you the hassle of writing code to copy an A or B to a new Common, because automapper can be set up to understand A->Common and then copy the matching property names. With some extra config it can also copy mismatched properties
Related
I have two lists: List a, List b
var a1= new A
{
Name = "XYZ",
Id = "123"
};
var a2= new A
{
Name = "UVW",
Id = "567"
};
var a = new List<A>()
{
a1,
a2
};
public class A
{
public string Name{ get; set; }
public string Id{ get; set; }
}
var b1= new B
{
Location = "US",
Id = "123"
};
var b2= new B
{
Location = "IN",
Id = "567"
};
var b = new List<B>()
{
b1,
b2
};
public class B
{
public string Location{ get; set; }
public string Id{ get; set; }
}
Notice that Id is common in both A and B classes. The final goal is to have a list that contains values of members from both A and B classes:
var output = new List<AB>()
{
ab1,
ab2
}
public class AB
{
public string Id{ get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
Or update List a to include values from List b?
How would I do that in C#?
You could use Join to get common data based on Id and populate AB, like the following code :
var output = aList.Join(bList,
a => a.Id,
b => b.Id,
(a, b) => new AB
{
Id = a.Id,
Location = b.Location,
Name = a.Name
}).ToList();
Demo
foreach(var item in output)
{
Console.WriteLine($"Id:{item.Id}, Name : {item.Name}, Location:{item.Location}");
}
Result:
Id:123, Name : XYZ, Location:US
Id:567, Name : UVW, Location:IN
Demo in dotnetfiddle : https://dotnetfiddle.net/3ZbK6c
I hope you find this helpful.
I am having two lists
class A{ int id; string name; string type; List<B> listB;}
class B{int id; int refid; string value;}
ListofA[{id=1,name="ABC",type="A",listB=null},id=1,name="ABC",type="A",listB=null}]
ListofB[{id=4,refid=1,value="ABC"},{id=5,refid=1,value="DEF"},{id=6,refid=2,value="XYZ"},]
I want to add matching objects of B to listB of class A. condition should be A.id == B.id. I tried below approach but I am getting blank list.
ListofA = ListofA.Select(a => new A { id = a.id, name =a.name,type = a.type,
listB = listOfB.Where(b => b.refid == a.id).ToList()}).ToList();
Is there anything missed from me.
Can any one please help me in this?
Thanks in advance
I have rewritten your code removing the whole syntax errors there. So you will get the desired output (no blank list):
class A {public int id; public string name; public string type;public List<B> listB; }
class B { public int id;public int refid; public string value; }
static void Main(string[] args)
{
List<A> ListofA = new List<A>{
new A() { id = 1,name = "ABC",type = "A",listB = null},
new A() { id = 2,name = "XYZ",type = "A",listB = null}
};
List<B> listOfB = new List<B>{
new B() { id =4,refid=1,value="ABC"},
new B() { id=5,refid=1,value="DEF"},
new B() { id=6,refid=2,value="XYZ"}} ;
var ListofA2 = ListofA.Select(a => new A
{
id = a.id,
name = a.name,
type = a.type,
listB = listOfB.Where(b => b.refid == a.id).ToList()
}
).ToList();
}
I made a silly mistake, because of that I was getting blank list. I made the changes and its working good now. Thanks for your help
Trying to figure out how to query an IEnumerable<T> using LINQ. The following simple example without IEnumerable works fine:
class Category
{
public string Title { get; set; }
public NameValue SubCategory { get; set; }
}
class NameValue
{
public string Name { get; set; }
public string Value { get; set; }
}
private static void testLinq()
{
Category[] categories = {
new Category { Title ="Abc", SubCategory = new NameValue { Name = "A", Value = "5"} },
new Category { Title ="Xyz", SubCategory = new NameValue { Name = "B", Value = "10" } }
};
IEnumerable<Category> q = categories.OrderBy(c => c.Title).ThenBy(c => c.SubCategory.Name);
foreach (Category c in q)
{
Console.WriteLine("{0} - {1}", c.Title, c.SubCategory.Name);
}
}
When I change the signature to have an IENumerable<NameValue> instead then I cannot access c.SubCategory.Name:
class Category
{
public string Title { get; set; }
public IEnumerable<NameValue> SubCategory { get; set; }
}
// For example, below does not compile:
IEnumerable<Category> q = categories.OrderBy(c => c.Title).ThenBy(c => c.SubCategory.Name);
// Also, this initialization of course won't work either
Category[] categories = {
new Category { Title ="Abc", SubCategory = new NameValue { Name = "A", Value = "5"} },
new Category { Title ="Xyz", SubCategory = new NameValue { Name = "B", Value = "10" } }
};
The error is:
IEnumerable' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)
Do I need to do a cast of some sort?
Update:
Output should be something like:
Abc (category)
A (sub)
B (sub)
C (...)
Xyz
B
K
M
Xyz2
A
Q
Z
In SQL I would do like something like this:
SELECT c.Title, s.Name, s.Value FROM Categories c
INNER JOIN SubCategory s ON
c.CategoryID = s.CategoryID
ORDER BY c.Title, s.Name -- sorting first on Category.Title, then on SubCategory.Name
Your SubCategory will be a collection so you cannot do it using ThenBy. You need to order the Category(s) and then order their SubCategory(s) like this. Note I added two SubCategory(s) to the first Category but their order is not correct. After we order them, then they will be correct:
Category[] categories = {
new Category { Title ="Abc", SubCategory = new List<NameValue>
{ new NameValue { Name = "B", Value = "5"},
new NameValue { Name = "A", Value = "5"} } },
new Category { Title ="Xyz", SubCategory = new List<NameValue>
{ new NameValue { Name = "A", Value = "10" } } }};
// First order by categories
var cats = categories.OrderBy(c => c.Title)
// Then create a new category and order by sub categories
.Select(x => new Category { Title = x.Title,
SubCategory = x.SubCategory.OrderBy(y => y.Name) });
If you can get away with only sorting the children when you need to use them, sorting by the parent and then sorting the children upon use like this would be fairly efficient:
public void DisplayA(A value)
{
Console.WriteLine(value.Name);
foreach (var child in value.Children.OrderBy(c => c.Name))
{
Console.WriteLine(string.Format("- {0}", child.Name));
}
}
Or if you want to avoid that, you could add a sorted property to the class. Since it's Linq, it will only be evaluated when you iterate through the list.
public class A
{
public string Name { get; set; }
public IEnumerable<B> Children { get; set; }
public IEnumerable<B> SortedChildren { get { return Children.OrderBy(ca => ca.Name); } }
}
public class B
{
public string Name { get; set; }
}
If they don't work for you, you could try these, but they won't be so efficient since you're creating new objects.
// This will flatten it into a single object, sorted by one field and the the other. Since this is Linq, it will create these new flattened objects each time you iterate through the IEnumerable.
public IEnumerable<FlattenedA> GetSortedFlattened(IEnumerable<A> collection)
{
var flattened = collection.SelectMany(a => a.Children.Select(ca => new FlattenedA() { Name = a.Name, SubName = ca.Name }));
var sorted = flattened.OrderBy(f => f.Name).ThenBy(f => f.SubName);
return sorted;
}
// This will return objects of A, where the child enumerable has been replaced with an OrderBy. Again this will return new objects each time you iterate through. Only when you iterate through the children will they be sorted.
public IEnumerable<A> GetSortedNonFlattened(IEnumerable<A> collection)
{
var withSortedChildren = collection.Select(a => new A() { Name = a.Name, Children = a.Children.OrderBy(ca => ca.Name) });
var sorted = withSortedChildren.OrderBy(a => a.Name);
return sorted;
}
public class FlattenedA
{
public string Name { get;set }
public string SubName { get; set; }
}
public class A
{
public string Name { get; set; }
public IEnumerable<B> Children { get; set; }
}
when you are setting it as IEnumerable you can't do this
SubCategory = new NameValue { Name = "A", Value = "5"}
you should use some implementation of IEnumerable,
like List<>
so it should be something like this
SubCategory = new List<NameValue>{new NameValue { Name = "A", Value = "5"}, addmore here};
and for your order linq, i would do this,
var OrderedCategories = categories.select(g =>
new Category{ Name = g.Name, subcategories = g.subcategories.orderby(h => h.Name) });
That's because your SubCategory now is no longer a simple single instance of NameValue, but rather an enumeration of those. So now you need to specify how to .ThenBy over a collection of .Names.
I have following objects
class A
{
public List<B> listB { get; set; }
}
class B
{
public int id { get; set; }
}
and in my application I have a scenario like below..
public void main()
{
var lstA = new List<A>();
var lstA = new List<A>();
var a1 = new A();
a1.listB = new List<B>
{
new B() { id = 1 },
new B() { id = 2 }
};
lstA.Add(a1);
a1 = new A();
a1.listB = new List<B>
{
new B() { id = 3 },
new B() { id = 4 }
};
lstA.Add(a1);
}
And I need to select all id's of B objects from lstA
Here is what I've tried so far
var ids = lst.Select(x=>x.listB.Select(y=>y.id)).ToList();
But It gives me a compilation error.
How can I do this?
You have to use SelectMany which flattens the lists:
var ids = lst.SelectMany(x => x.listB.Select(y => y.id)).ToList();
You are almost there, use SelectMany
var ids = lst.SelectMany(x=>x.listB.Select(y=>y.id)).ToList();
Check your Working Code
Here's what i did and it works perfectly
All i did was make the classes public and when you initialise List<B>, you add new List<B> because even though intellisense doesn't show you any error, when you run the application, you get object not referenced error
class Program
{
static void Main(string[] args)
{
var lstA = new List<A>();
var a1 = new A()
{
listB = new List<B>()
{
new B
{
id = 3
},
new B
{
id = 5
}
}
};
var a2 = new A()
{
listB = new List<B>()
{
new B
{
id = 1
},
new B
{
id = 8
}
}
};
lstA.Add(a1);
lstA.Add(a2);
var ids = lstA.SelectMany(r => r.listB.Select(x => x.id));
foreach (var id in ids)
{
Console.WriteLine(id);
}
Console.ReadKey();
}
}
public class A
{
public List<B> listB { get; set; }
}
public class B
{
public int id { get; set; }
}
try this to ignore duplicate id
var ids = lstA.SelectMany(x => x.listB.Select(y => y.id)).Distinct().ToList();
I have 2 objects:
public class ClassA
{
public int Id
public string name;
public ClassB myObjectB;
}
public class ClassB
{
public int Id
public string name
}
Having 2 Lists for <ClassA> <ClassB>
Some items from List1 match by Id with an item on List2... I want to set the objectB foreach item...
foreach(ClassA item in List1)
{
ClassB obj = (from b in List2 where b.Id == item.Id select b).SingleOrDefault()
if(obj != null)
{
item.myObjectB = obj;
////////break; <- ignore this
}
}
This solution works for me, but I'm just wondering if there is a better way to do this, instead of Foreach
Thanks everyone for your help!!!
I think that a foreach, in this case, is actually the appropriate approach, as you're mutating your list. You could potentially simplify your code a bit, however:
foreach(ClassA item in List1)
{
item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id);
}
This will set the item every time, though it will be set to null if there is no match. If you already have items in myObjectB and setting them to null is inappropriate, you could use:
foreach(ClassA item in List1)
{
item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id) ?? item.myObjectB;
}
Expanding on Reed's answer.. You can actually do this in a one-liner, because a list has a ForEach method.
List1.ForEach(item => item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id) ?? item.myObjectB);
List<ClassA> list1 = new List<ClassA>();
List<ClassB> list2 = new List<ClassB>();
list1.Add(new ClassA { Id = 2, name = "a2" });
list1.Add(new ClassA { Id = 3, name = "a3" });
list1.Add(new ClassA { Id = 4, name = "a4" });
list1.Add(new ClassA { Id = 5, name = "a5" });
list2.Add(new ClassB { Id = 1, name = "b1" });
list2.Add(new ClassB { Id = 2, name = "b2" });
list2.Add(new ClassB { Id = 4, name = "b4" });
list2.Add(new ClassB { Id = 5, name = "b5" });
// Goal is to set ClassA::myObjectB from List1 to
// matching instance (if any) of ClassB from List2
var query =
from a in list1
from b in list2
where a.Id == b.Id
select Tuple.Create(a, b);
foreach (var element in query)
element.Item1.myObjectB = element.Item2;
Update:
Or if you really don't want a for loop, I just realized you can use the fact that assignments return a value and at the same time make an entry in an obfuscated code contest :)
(from a in list1
from b in list2
where a.Id == b.Id
select a.myObjectB = b).ToList();
Update2:
I just thought of an alternate approach - depending on your scenario, a lazy mechanism might work for you?
public class ClassA
{
public int Id
public string name;
private ClassB myObjectB;
public ClassB MyObjectB {
get { return myObjectB ?? (myObjectB = list2.FirstOrDefault(x => this.Id == x.Id)); }
}
}
With classes defined as follows:
class ClassA {
public int Id { get; private set; }
public string name { get; private set; }
public ClassB myObjectB { get; set; }
public ClassA(int pId, string pName) {
Id = pId;
name = pName;
}
}
class ClassB {
public int Id { get; private set; }
public string name { get; private set; }
public ClassB(int pId, string pName) {
Id = pId;
name = pName;
}
}
You can do the following using the LINQ Join method:
var listA = new List<ClassA> {
new ClassA(1, "OneA"),
new ClassA(2, "TwoA"),
new ClassA(3, "ThreeA")
};
var listB = new List<ClassB> {
new ClassB(1, "OneB"),
new ClassB(2, "TwoB"),
new ClassB(4, "FourB")
};
listA
.Join(
listB,
itemA => itemA.Id,
itemB => itemB.Id,
(itemA, itemB) => new { ItemA = itemA, ItemB = itemB }
).ForEach(pair => pair.ItemA.myObjectB = pair.ItemB);
listA.ForEach(itemA => Console.WriteLine(
"{0} maps to {1}",
itemA == null
? "null"
: itemA.name,
(itemA == null || itemA.myObjectB == null)
? "null"
: itemA.myObjectB.name
));
Output is:
OneA maps to OneB
TwoA maps to TwoB
ThreeA maps to null