need to group data based on `InstanceData` Name property - c#

I have one Packet like below,
var dataPacket = new Packet
{
Id = new Guid("2e08bd98-68eb-4358-8efb-9f2adedfb034"),
Results = new Result
{
ResultName = "ResultName1",
Instances = new List<Instance>
{
new Instance
{
InstanceName = "InstanceName1",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V1"},
new InstanceData{Name = "N2", Value = "V2"}
}
},
new Instance
{
InstanceName = "InstanceName2",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V3"},
new InstanceData{Name = "N2", Value = "V4"}
}
}
}
}
};
Here are the class structures,
public class Packet
{
public Guid Id { get; set; }
public Result Results { get; set; }
}
public class Result
{
public string ResultName { get; set; }
public List<Instance> Instances { get; set; }
}
public class Instance
{
public string InstanceName { get; set; }
public List<InstanceData> InstanceDatas { get; set; }
}
public class InstanceData
{
public string Name { get; set; }
public string Value { get; set; }
}
For above Packet I want to spilt this into 2 Packets based on InstanceData common Name
All N1 from InstanceName1 and InstanceName2 into one packet
All N2 from InstanceName1 and InstanceName2 into one packet
Packet1 should be like this,
var packet1 = new Packet
{
Id = new Guid("2e08bd98-68eb-4358-8efb-9f2adedfb034"),
Results = new Result
{
ResultName = "ResultName1",
Instances = new List<Instance>
{
new Instance
{
InstanceName = "InstanceName1",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V1"},
}
},
new Instance
{
InstanceName = "InstanceName2",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V3"},
}
}
}
}
};
and similarly packet2.
I have tried below, but this will split on InstanceData as well and giving 4 packets.
var packets = dataPacket.Results
.Instances
.SelectMany(x =>
x.InstanceDatas.Select(y => new Packet()
{
Id = dataPacket.Id,
Results = new Result()
{
ResultName = dataPacket.Results.ResultName,
Instances = new List<Instance>()
{
new Instance()
{
InstanceDatas = new List<InstanceData>() {y},
InstanceName = x.InstanceName
}
}
}
}));

You can write a helper method which finds the possible names as keys and iterate over the keys. Then you build new object instances for each key you are checking. The source code can look like this:
private static IList<Packet> SplitByName(Packet packet) {
IList<string> names = packet.Results.Instances
.SelectMany(it => it.InstanceDatas)
.Select(it => it.Name)
.Distinct()
.ToList();
IList<Packet> result = new List<Packet>();
foreach (string name in names)
{
List<Instance> newInstances = packet.Results.Instances
.Select(it => new Instance {
InstanceName = it.InstanceName,
InstanceDatas = it.InstanceDatas
.Where(it => it.Name == name)
.ToList()
})
.Where(it => it.InstanceDatas.Any())
.ToList();
Result newResult = new Result {
ResultName = packet.Results.ResultName,
Instances = newInstances
};
result.Add(new Packet {
Id = packet.Id,
Results = newResult
});
}
return result;
}
For each name you are filtering the InstanceData instances for each Instance object. Depending on your needs you might want to add .Where(it => it.InstanceData.Any()) so you don't have any "empty" instances.

Related

how to combined "collectionResult" based on DataType

I have below class structure,
public class CollectionProperty
{
public string Name { get; set; }
public object Value { get; set; }
public string DataType { get; set; }
}
public class Instance
{
public string Name { get; set; }
public List<CollectionProperty> CollectionProperties { get; set; }
}
public class CollectionResult
{
public string Asset { get; set; }
public List<Instance> Instances { get; set; }
}
I have below collection result with 2 instance having various collection property with 2 data types double and string.
var collectionResult = new CollectionResult
{
Asset = "A1",
Instances = new List<Instance>
{
new Instance
{
Name = "Instance-1",
CollectionProperties = new List<CollectionProperty>
{
new CollectionProperty {Name = "N1", Value = 10, DataType = "Double"},
new CollectionProperty {Name = "N2", Value = "S1", DataType = "String"}
}
},
new Instance
{
Name = "Instance-2",
CollectionProperties = new List<CollectionProperty>
{
new CollectionProperty {Name = "N1", Value = 20, DataType = "Double"},
new CollectionProperty {Name = "N2", Value = "S2", DataType = "String"}
}
}
}
};
Now my goal to split the collection result various instance and group them based on data type. With below code I am able to split put but it's giving me 4 collection results, but it's should give 2 (2 for double and 2 for string), please suggest. Thanks!
var X = collectionResult.Instances
.SelectMany(collectionInstance => collectionInstance.CollectionProperties,
(collectionInstance, collectionProperty) =>
new
{
CollectionResult = new CollectionResult
{
Asset = collectionResult.Asset,
Instances = new List<Instance>
{
new Instance
{
Name = collectionInstance.Name,
CollectionProperties = new List<CollectionProperty>
{
collectionProperty
}
}
},
},
Instance = collectionInstance.Name,
Property = collectionProperty.Name,
DataType = collectionProperty.DataType
});
You just need to simply add a GroupBy statement after your selectMany:
var X = collectionResult.Instances
.SelectMany(collectionInstance => collectionInstance.CollectionProperties,
(collectionInstance, collectionProperty) =>
new
{
CollectionResult = new CollectionResult
{
Asset = collectionResult.Asset,
Instances = new List<Instance>
{
new Instance
{
Name = collectionInstance.Name,
CollectionProperties = new List<CollectionProperty>
{
collectionProperty
}
}
},
},
Instance = collectionInstance.Name,
Property = collectionProperty.Name,
DataType = collectionProperty.DataType
}).GroupBy(c => c.DataType);
In this way you will have two groups, which have been grouped with double and string keys:

How to convert a list to JSON in C#?

I have a City_State list:
City_State[0].Range="\"city\":\"REDMOND\",\"state\":\"AK\"";
City_State[1].Range="\"city\":\"Alex City\",\"state\":\"
How to convert it into json like below:
var _pairs = new
{
criteria = new { cities = new[] { new { city = "REDMOND", state = "WA" },
new { city = "Alex City", state = "AL" } }
} ;
I tried the code below, but it does not work:
var _pairs = new { criteria = new { cities = new[] { _paged_City_State.ToArray() } } };
If you had these classes:
public class CityStateRaw
{
public string Range { get; set; }
}
public class CityState
{
public string City { get; set; }
public string State { get; set; }
}
The following code would work:
var ranges = new[]
{
new CityStateRaw { Range = "{\"city\":\"REDMOND\",\"state\":\"AK\"}" },
new CityStateRaw { Range = "{\"city\":\"Alex City\",\"state\":\"foo\"}" },
};
var list = ranges
.Select(raw => JsonConvert.DeserializeObject<CityState>(raw.Range))
.ToList();
But if this doesn't match your expectations you should be more concrete about what your exact input and the expected output should be.

Compare each class attribute value using LINQ

I have a below class. I will get two objects List<Client> data1 and List<Client> data2. I want to compare data1 and data2 with each of the attribute value.
For example, if data1 object has the LastName=a and ClientId=1,..etc and if data2 list has the same set of data i want to push that into one more list.
Any idea, how can we achieve this using LINQ/Minimal code?
public class Client
{
public int ClientId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
}
using Intersect
List<Client> data1 = new List<Client>();
List<Client> data2 = new List<Client>();
List<Client> newlst = new List<Client>();
Client obj = new Client();
obj.ClientId = 1;
obj.LastName = "a";
obj.FirstName = "n";
obj.Email = "e";
data1.Add(obj);
data2.Add(obj);
obj = new Client();
obj.ClientId = 2;
obj.LastName = "a";
obj.FirstName = "f";
obj.Email = "e";
data1.Add(obj);
newlst = data1.Intersect(data2).ToList();
I have used IEqualityComparer which is used to compare both the collection and Intersect will give the common value.I have tested the code for few scenario. You can check for all the scenario.
Hope this code will be helpful.
namespace UnitTestProject
{
[TestClass]
public class CompareTwoGenericList
{
[TestMethod]
public void TestMethod1()
{
var coll = GetCollectionOne();
var col2 = GetCollectionTwo();
//Gives the equal value
var commonValue = coll.Intersect(col2, new DemoComparer()).ToList();
//Difference
var except=coll.Except(col2, new DemoComparer()).ToList();
}
public List<Demo> GetCollectionOne()
{
List<Demo> demoTest = new List<Demo>()
{
new Demo
{
id=1,
color="blue",
},
new Demo
{
id=2,
color="green",
},
new Demo
{
id=3,
color="red",
},
};
return demoTest;
}
public List<Demo> GetCollectionTwo()
{
List<Demo> demoTest = new List<Demo>()
{
new Demo
{
id=1,
color="blue",
},
new Demo
{
id=2,
color="green",
},
new Demo
{
id=4,
color="red",
},
};
return demoTest;
}
}
// Custom comparer for the Demo class
public class DemoComparer : IEqualityComparer<Demo>
{
// Products are equal if their color and id are equal.
public bool Equals(Demo x, Demo y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the demo properties are equal.
return x.color == y.color && x.id == y.id;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(Demo demo)
{
//Check whether the object is null
if (Object.ReferenceEquals(demo, null)) return 0;
//Get hash code for the color field if it is not null.
int hashColor = demo.color == null ? 0 : demo.color.GetHashCode();
//Get hash code for the id field.
int hashId = demo.id.GetHashCode();
//Calculate the hash code for the product.
return hashColor ^ hashId;
}
}
}
Create Your new class ClientView
public class ClientView{
public int ClientId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
}
List lst = new List();
var data = from n in db.client
select new ClientView()
{
ClientId = n.ClientId ,
LastName = n.LastName ,
FirstName = n.FirstName,
};
var data1 = from n in db.client
select new ClientView()
{
ClientId = n.ClientId ,
LastName = n.LastName ,
FirstName = n.FirstName,
};
lst.AddRange(data);
lst.AddRange(data1);
List<ClientView> lst1 = new List<ClientView>();
foreach (var singlelst in lst)
{
ClientView newClient = new ClientView ();
newClient.Id = singlelst.Id;
newClient.આપેલ = singlelst.LastName;
newClient.આપેલતારીખ = singlelst.FirstName;
lst1.Add(newClient);
}
Try this:
public IEnumerable<PropertyInfo> GetVariance(Client user)
{
foreach (PropertyInfo pi in user.GetType().GetProperties()) {
object valueUser = typeof(Client).GetProperty (pi.Name).GetValue (user);
object valueThis = typeof(Client).GetProperty (pi.Name).GetValue (this);
if (valueUser != null && !valueUser.Equals(valueThis))
yield return pi;
}
}
IEnumerable<PropertyInfo> variances = data1.GetVariance (data2);
foreach (PropertyInfo pi in variances)
Console.WriteLine (pi.Name);

C# - Filling List with LINQ from another List<>

Here's my main class:
public class Subject
{
public struct Class
{
public byte Day { get; set; }
public DateTime Time { get; set; }
}
public string Name { get; set; }
public List<Class> Data { get; set; }
}
For example,
List<Subject> subjects = new List<Subject>
{
new Subject()
{
Name = "Math",
Data = new List<Class>()
{
new Class { Day = 2, Time = Convert.ToDateTime("8:30") },
new Class { Day = 2, Time = Convert.ToDateTime("10:25") }
}
},
new Subject()
{
Name = "Astronomy",
Data = new List<Class>()
{
new Class { Day = 2, Time = Convert.ToDateTime("12:30") },
new Class { Day = 4, Time = Convert.ToDateTime("14:30") }
}
},
new Subject()
{
Name = "Chemistry",
Data = new List<Class>()
{
new Class { Day = 3, Time = Convert.ToDateTime("8:30") }
}
},
new Subject()
{
Name = "Physics",
Data = new List<Class>()
{
new Class { Day = 3, Time = Convert.ToDateTime("10:25") },
new Class { Day = 4, Time = Convert.ToDateTime("12:30") }
}
}
};
The data above is filling up by parsing JSON.
But now I need to do next:
1.Select all distinct Day (in this case: 2, 3, 4);
2. Fill this days with subject (Name and Time).
I created this:
public class Schedule
{
public struct Subject
{
public string Name { get; set; }
public DateTime Time { get; set; }
}
public struct Day
{
public byte DayOfWeek { get; set; }
public List<Subject> Subjects { get; set; }
}
public List<Day> Days { get; set; }
}
so I except to do something like this:
Schedule schedule = new Schedule();
schedule.Days = new List<Schedule.Day>()
{
new Schedule.Day()
{
DayOfWeek = 2,
Subjects = new List<Schedule.Subject>()
{
new Schedule.Subject() { Name = "Math", Time = Convert.ToDateTime("8:30") },
new Schedule.Subject() { Name = "Math", Time = Convert.ToDateTime("10:25") },
new Schedule.Subject() { Name = "Astronomy", Time = Convert.ToDateTime("12:30") }
}
},
new Schedule.Day()
{
DayOfWeek = 3,
Subjects = new List<Schedule.Subject>()
{
new Schedule.Subject() { Name = "Chemistry", Time = Convert.ToDateTime("8:30") },
new Schedule.Subject() { Name = "Physics", Time = Convert.ToDateTime("10:25") },
}
},
new Schedule.Day()
{
DayOfWeek = 4,
Subjects = new List<Schedule.Subject>()
{
new Schedule.Subject() { Name = "Physics", Time = Convert.ToDateTime("12:30") },
new Schedule.Subject() { Name = "Astronomy", Time = Convert.ToDateTime("14:30") }
}
}
};
The question is How I can select data to Schedule from List<Subjects> with LINQ (I don't wanna use loops).
var result = subjects
.SelectMany(s => s.Data.Select(x => new { s.Name, x.Day, x.Time }))
.GroupBy(x => x.Day)
.Select(g => new Schedule.Day
{
DayOfWeek = g.Key,
Subjects = g.Select(item => new Schedule.Subject
{
Name = item.Name,
Time = item.Time
})
.OrderBy(item => item.Time)
.ToList()
})
.OrderBy(gItem => gItem.DayOfWeek)
.ToList();
And then....
Schedule schedule = new Schedule();
schedule.Days = result;
It's also weird to put the List<Day> inside the schedule class.
Out of fun I've rewritten the solution to promote the Query-Expression Syntax.
IEnumerable<Schedule.Day> scheduledDays =
from subj in subjList
from cl in subj.Data
select new { Class = cl, SubjectName = subj.Name } into classWithSubject
group classWithSubject by classWithSubject.Class.Day into classesByDay
orderby classesByDay.Key
select new Schedule.Day()
{
DayOfWeek = classesByDay.Key,
Subjects = (from cl in classesByDay
orderby cl.Class.Time
select new Schedule.Subject() { Name = cl.SubjectName, Time = cl.Class.Time }).ToList()
};
Schedule sched = new Schedule() { Days = scheduledDays.ToList() };
If I right understood, You can use ForEach LINQ:
schedule.Days.ForEach(x => x.Subjects.ForEach(y => Console.WriteLine($"{y.Name}, {y.Time}")));
Instead of
Console.WriteLine($"{y.Name}, {y.Time}")
You can write this info into some variables.
P.S. ForEach will pass all elements of list.
Try this:
var newQuery = subjects.SelectMany(y => y.Data.Select(x=> new {x.Day,x.Time,y.Name }));
var newQuery2 = newQuery.OrderBy(x=>x.Day).OrderBy(x=>x.Time).GroupBy(x => x.Day);
var newQuery3 = newQuery2.Select(x => new Schedule.Day
{
DayOfWeek = x.Key,
Subjects = x.Select(y => new Schedule.Subject
{
Time = y.Time,
Name = y.Name
}).ToList()
});

Linq : Comparing 1 Child Collection to (Aggregated) ChildCollection(s)

I have a Linq question: (DotNet Framework 4.0)
I have the following classes:
public class Employee
{
public Guid? EmployeeUUID { get; set; }
public string SSN { get; set; }
}
public class JobTitle
{
public Guid? JobTitleSurrogateKey { get; set; }
public string JobTitleName { get; set; }
}
public class EmployeeToJobTitleMatchLink
{
public EmployeeToJobTitleMatchLink()
{
this.TheJobTitle = new JobTitle() { JobTitleSurrogateKey = Guid.NewGuid(), JobTitleName = "SomeJobTitle:" + Guid.NewGuid().ToString("N") };
}
public Guid LinkSurrogateKey { get; set; }
/* Related Objects */
public Employee TheEmployee { get; set; }
public JobTitle TheJobTitle { get; set; }
}
public class Organization
{
public Organization()
{
this.Links = new List<EmployeeToJobTitleMatchLink>();
}
public int OrganizationSurrogateKey { get; set; }
public ICollection<EmployeeToJobTitleMatchLink> Links { get; set; }
}
In my code below, I can compare 2 child-collections and get the results I need (in "matches1".
Here I am using the "SSN" string property to compare and find the overlaps. And the Console.Write for matches1 works as I expect.
What I don't know how to do is compare the first child collection (org10) to all the children in (allOtherOrgsExceptOrg10 (all the Organizations and all the Links of these Organizations )
The commented out code shows kinda what I'm trying to do, one of my many feeble attempts today.
But basically, match2 would be populated with all the SSN overlaps...but comparing org10 with allOtherOrgsExceptOrg10, all their "Links", and their Employee.SSN's.
org10 overlaps with org20 with "AAA", so match2 would contain "AAA". and org10 overlaps with org30 with "BBB" so match2 would contain "BBB".
Organization org10 = new Organization();
org10.OrganizationSurrogateKey = 10;
Employee e11 = new Employee() { SSN = "AAA", EmployeeUUID = new Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") };
EmployeeToJobTitleMatchLink link11 = new EmployeeToJobTitleMatchLink();
link11.TheEmployee = e11;
org10.Links.Add(link11);
Employee e12 = new Employee() { SSN = "BBB", EmployeeUUID = new Guid("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB") };
EmployeeToJobTitleMatchLink link12 = new EmployeeToJobTitleMatchLink();
link12.TheEmployee = e12;
org10.Links.Add(link12);
Organization org20 = new Organization();
org20.OrganizationSurrogateKey = 20;
Employee e21 = new Employee() { SSN = "AAA", EmployeeUUID = new Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") };
EmployeeToJobTitleMatchLink link21 = new EmployeeToJobTitleMatchLink();
link21.TheEmployee = e21;
org20.Links.Add(link21);
Employee e22 = new Employee() { SSN = "CCC", EmployeeUUID = new Guid("CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC") };
EmployeeToJobTitleMatchLink link22 = new EmployeeToJobTitleMatchLink();
link22.TheEmployee = e22;
org20.Links.Add(link22);
Organization org30 = new Organization();
org30.OrganizationSurrogateKey = 30;
Employee e31 = new Employee() { SSN = "BBB", EmployeeUUID = new Guid("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB") };
EmployeeToJobTitleMatchLink link31 = new EmployeeToJobTitleMatchLink();
link31.TheEmployee = e31;
org30.Links.Add(link31);
Employee e32 = new Employee();
e32.SSN = "ZZZ";
EmployeeToJobTitleMatchLink link32 = new EmployeeToJobTitleMatchLink();
link32.TheEmployee = e32;
org30.Links.Add(link32);
IList<Organization> allOtherOrgsExceptOrg10 = new List<Organization>();
/* Note, I did not add org10 here */
allOtherOrgsExceptOrg10.Add(org20);
allOtherOrgsExceptOrg10.Add(org30);
IEnumerable<EmployeeToJobTitleMatchLink> matches1 =
org10.Links.Where(org10Link => org20.Links.Any(org20Link => org20Link.TheEmployee.SSN.Equals(org10Link.TheEmployee.SSN, StringComparison.OrdinalIgnoreCase)));
IEnumerable<EmployeeToJobTitleMatchLink> matches2 = null;
//org10.Links.Where(org10Link => ( allOtherOrgs.Where ( anyOtherOrg => anyOtherOrg.Links.Any(dbSideChild => dbSideChild.TheEmployee.SSN == org10Link.TheEmployee.SSN)) );
if (null != matches1)
{
foreach (EmployeeToJobTitleMatchLink link in matches1)
{
Console.WriteLine(string.Format("matches1, SSN = {0}", link.TheEmployee.SSN));
}
}
if (null != matches2)
{
foreach (EmployeeToJobTitleMatchLink link in matches2)
{
Console.WriteLine(string.Format("matches2, SSN = {0}", link.TheEmployee.SSN));
}
}
matches2 =
allOtherOrgsExceptOrg10.SelectMany(x => x.Links)
.Where(x => org10.Links.Select(o => o.TheEmployee.SSN).Contains(x.TheEmployee.SSN));
You can use the SelectMany on the allOther collection to select all Links over all org's. Then check if any SSN is inside the org10 List.
See: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.selectmany(v=vs.100).aspx
You can use SelectMany to flatten out the collection and then use it just like you have for matches1
IEnumerable<EmployeeToJobTitleMatchLink> matches2 =
org10.Links.Where(
org10Link =>
allOtherOrgsExceptOrg10.SelectMany(allOtherOrgs => allOtherOrgs.Links).Any(
anyOtherLink =>
anyOtherLink.TheEmployee.SSN.Equals(org10Link.TheEmployee.SSN, StringComparison.OrdinalIgnoreCase)));
The SelectMany will make it seem like one IEnumerable instead of and IEnumerable of an IEnumerable.

Categories

Resources