I have Collection:
public class Test
{
public double Index { set; get; }
public double A { set; get; }
public double B { set; get; }
public double C { set; get; }
}
List<Test> test = new List<Test>();
I filled A and B with some random numbers. After that, I want add items C in collection. For example, what I trying:
foreach (Test t in test)
{
c = t.A + t.B;
test.Add(new Test {C = c }); <------
}
How can I add element C on same position like A and B? (Sorry for my english)
If you want C to be the sum of A and B it might be better to use a computed field:
public class Test
{
public double Index { set; get; }
public double A { set; get; }
public double B { set; get; }
public double C
{
get
{
return A + B;
}
}
}
So
Test test = new Test()
{
A = 1;
B = 2;
};
test.C == 3; //true
the advantage of the above is that C is always the sum of the other values and you don't need to recompute it every time. So if I take above and do:
test.B = 3; //instead of the original 2
Then C stays in sync:
test.C == 3; //false
test.C == 4; //now true
To do this with your original method (or the other answer) means re-looping the result set.
Or in C# 6.0 you can use an expression-bodied property
public class Test
{
public double Index { set; get; }
public double A { set; get; }
public double B { set; get; }
public double C => A + B;
}
foreach (Test t in test)
{
t.C = t.A + t.B;
}
just set the property to the equation
Related
I have the following object:
namespace BluetoothExample
{
public class Assay
{
public double Band_1 //Vis450
{
get;
set;
}
public double Band_2 //Vis500
{
get;
set;
}
public double Band_3 //Vis550
{
get;
set;
}
public double Band_4 //Vis570
{
get;
set;
}
}
}
I want to populate the 4 bands in my object. Which I currently do in the following way:
public Populate()
{
int _i = 0;
double[] nirData = new double[4];
MyDevice.Characteristic.ValueUpdated += (sender, e) =>
{
nirData[_i] = BitConverter.ToDouble(e.Characteristic.Value, 0);
_i++;
};
Assay assay = new Assay();
assay.Band_1 = nirData[0];
assay.Band_2 = nirData[1];
assay.Band_3 = nirData[2];
assay.Band_4 = nirData[3];
}
I was wondering if it was possible to do the entire thing inside the MyDevice.Characteristic.ValueUpdate method instead? My thought is that it should be possible to increment and populate the properties of my object like so:
string name = "assay.Band_" + _i;
name = BitConverter.ToDouble(e.Characteristic.Value, 0);
This is obviously wrong, but it sort of demonstrates my idea.
Just make your band an Array, or List:
public class Assay
{
public double[] Band
{
get;
set;
}
}
And then you can simply assign it like:
public Populate()
{
int _i = 0;
double[] nirData = new double[4];
MyDevice.Characteristic.ValueUpdated += (sender, e) =>
{
nirData[_i] = BitConverter.ToDouble(e.Characteristic.Value, 0);
_i++;
};
Assay assay = new Assay();
assay.Band = nirData;
}
Hey i want to ask if its possible to use List object property in GroupBy clausule:
MyProblem:
I want to group a list by a property directly from a list, and from object
which this list contains
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp8
{
class ExampleDocument
{
public int idDoc { get; set; }
public List<Payments> payments { get; set; }
public string date { get; set; }
public ExampleDocument(int a, List<Payments> paymentss,string d)
{
idDoc = a;
payments = paymentss;
d = date;
}
}
class Payments
{
public int value { get; set; }
public string method { get; set; }
public Payments(int a, string b)
{
value = a;
method = b;
}
}
class Program
{
List<ExampleDocument> exampleList = new List<ExampleDocument>();
List<Payments> listOfPayments = new List<Payments>();
Payments a = new Payments(1000, "cash");
Payments b = new Payments(2000, "card");
void Work()
{
listOfPayments.Add(a);
listOfPayments.Add(b);
exampleList.Add(new ExampleDocument(1, listOfPayments,"2018-08-06"));
}
static void Main(string[] args)
{
Program c = new Program();
c.exampleList.GroupBy(p => new { p.date, ((Payments)p.payments).method });
}
}
}
"((Payments)p.payments).method }" here i got exception
Cannot convert type
'System.Collections.Generic.List' to
'ConsoleApp8.Payments'
ExampleList - contains commercial documents.
p.Example_VARIABLE - is the date of issue of the document.
Payments is class which contains property of payments
payments is a list of all payments (in document)
method is a payment method
As Cetin Bastoz answer i managed new query
namespace ConsoleApp8
{
class ExampleDocument
{
public int idDoc { get; set; }
public int DocNumber { get; set; }
public List<Payments> payments { get; set; }
public string date { get; set; }
public ExampleDocument(int a, int docNumber, List<Payments> paymentss, string d)
{
idDoc = a;
payments = paymentss;
DocNumber = docNumber;
date = d;
}
}
class Payments
{
public int payID { get; set; }
public int value { get; set; }
public string method { get; set; }
public Payments(int PayID, int a, string b)
{
payID = PayID;
value = a;
method = b;
}
}
class Program
{
List<ExampleDocument> exampleList = new List<ExampleDocument>();
List<Payments> listOfPayments = new List<Payments>();
void Work()
{
Payments a = new Payments(2, 1000, "cash");
Payments b = new Payments(1, 2000, "card");
listOfPayments.Add(a);
listOfPayments.Add(b);
exampleList.Add(new ExampleDocument(1, 20189, listOfPayments, "2018-08-06"));
exampleList.Add(new ExampleDocument(1, 201810, listOfPayments, "2018-08-08"));
exampleList.Add(new ExampleDocument(1, 201811, listOfPayments, "2018-08-09"));
}
static void Main(string[] args)
{
Program c = new Program();
c.Work();
var data2 = from d in c.exampleList
from p in d.payments
select new
{
Number = d.DocNumber,
DocDate = d.date,
payID = p.payID,
Amount = p.value
} into s
group s by s.DocDate into g
select g;
var sDocDate = "2018-08-06";
var result = data2.Select(x => x.Where(p => p.DocDate.Equals(sDocDate)));
Console.ReadKey();
}
}
}
Everthing is clear, untill i want to compare date2 result to date given from string
var sDocDate = "2018-01-21"
var result = data2.Select(x => x.Where(p => p.DocDate.Equals(sDocDate)));
Why at bottom image "result" got those "1,2,3 variable without entries"
You would use OfType<>() to get ExampleObject types from an ArrayList (I thought nobody left still using ArrayList):
var grouped = exampleList.OfType<ExampleObject>()
.GroupBy(p => new { p.EXAMPLE_VARIABLE, p.ExampleProperty});
EDIT: Although your code is faulty, after correcting it:
var data = from d in exampleList
from p in d.payments
group p by new {DocDate=d.date, PayType=p.method} into g
select g;
I am looking for a way to store two List<int> objects. I am currently using a List<List<int>>, but that seems wasteful to only use [0] and [1].
Is there a better way of doing this?
I think you are looking for Tuples:
var t = Tuple.Create(new List<int>(), new List<int>());
Then just access t.Item1 and t.Item2.
Thanks to Servy to pointing out, I've missed the point.
OP wants to create composite List<int> + List<int>, rather than a List<int+int>.
public class Pair<T>
{
public T Left { get; set; }
public T Right { get; set; }
public IntPair(T left, T right)
{
this.Left = left;
this.Right = right;
}
}
//or you might want more flexibility
public class Pair<TLeft, TRight>
{
public TLeft Left { get; set; }
public TRight Right { get; set; }
public IntPair(TLeft left, TRight right)
{
this.Left = left;
this.Right = right;
}
}
Create your own data type to hold both integers
public class IntPair
{
public int A { get; set; }
public int B { get; set; }
public IntPair(int a, int b)
{
this.A = a;
this.B = b;
}
}
If the list isn't required to be returned, only used temporarily.
There is also the anonymous type.
//or generate this from the existing data
var pairs = Enumerable.Empty<object>()
.Select(o => new { A = 0, B = 0 })
.ToList();
for(int i = 0; i < 10; i++)
pairs.add(new { A = i, B = i * 2 });
//do more with pairs...
I have two objects of same type with different values:
public class Itemi
{
public Itemi()
{
}
public int Prop1Min { get; set; }
public int Prop1Max { get; set; }
public int Prop2Min { get; set; }
public int Prop2Max { get; set; }
public int Prop3Min { get; set; }
public int Prop3Max { get; set; }
...................................
public int Prop25Min { get; set; }
public int Prop25Max { get; set; }
}
Now I instantiate two objects of this type and add some values to their properties.
Itemi myItem1 = new Itemi();
myItem1.Prop1Min = 1;
myItem1.Prop1Max = 4;
myItem1.Prop2Min = 2;
myItem1.Prop2Max = 4;
myItem1.Prop3Min = -1;
myItem1.Prop3Max = 5;
.............................
myItem1.Prop25Min = 1;
myItem1.Prop25Max = 5;
Itemi myItem2 = new Itemi();
myItem2.Prop1Min = 1;
myItem2.Prop1Max = 5;
myItem2.Prop2Min = -10;
myItem2.Prop2Max = 3;
myItem2.Prop3Min = 0;
myItem2.Prop3Max = 2;
................................
myItem2.Prop25Min = 3;
myItem2.Prop25Max = 6;
What is the best and fastest way to do this comparison:
take each properties from myItem1 and check if values from Prop1-25 Min and Max are within the range values of myItem2 Prop1-25 Min and Max
Example:
myItem1.Prop1Min = 1
myItem1.Prop1Max = 4
myItem2.Prop1Min = 1
myItem2.Prop1Max = 5
this is True because mtItem1 Prop1 min and max are within the range of myItem2 min and max.
the condition should be AND in between all properties so in the end after we check all 25 properties if all of them are within the range of the second object we return true.
Is there a fast way to do this using Linq or other algorithm except the traditional if-else?
I would refactor the properties to be more along the lines of:
public class Item
{
public List<Range> Ranges { get; set; }
}
public class Range
{
public int Min { get; set; }
public int Max { get; set; }
}
Then your comparison method could be:
if (myItem1.Ranges.Count != myItem2.Ranges.Count)
{
return false;
}
for (int i = 0; i < myItem1.Ranges.Count; i++)
{
if (myItem1.Ranges[i].Min < myItem2.Ranges[i].Min ||
myItem1.Ranges[i].Max > myItem2.Ranges[i].Max)
{
return false;
}
}
return true;
Otherwise you will have to use Reflection, which is anything but fast.
Linq is using standart statements like if...then, for each and other, there is no magic :)
If the final goal only to compare, without needing to say, which properties are not in the range, then you not need to check them all, on the first unequals you can end checking.
Because you have so much properties, you must think about saving it in Dictionary, or List, for example. Or to use dynamic properties (ITypedList), if it will use for binding.
You really should do something like Ginosaji proposed.
But if you want to go with your current data model, here is how I would solve it. Happy typing. :)
public static bool RangeIsContained(int outerMin, int outerMax, int innerMin, int innerMax)
{
return (outerMin <= innerMin && outerMax >= innerMax);
}
public bool IsContained(Itemi outer, Itemi inner)
{
return RangeIsContained(outer.Prop1Min, outer.Prop1Max, inner.Prop1Min, inner.Prop1Max)
&& RangeIsContained(outer.Prop2Min, outer.Prop2Max, inner.Prop2Min, inner.Prop2Max)
// ...
&& RangeIsContained(outer.Prop25Min, outer.Prop25Max, inner.Prop25Min, inner.Prop25Max);
}
With your data model this is basically the only way to go except for reflection (slow!). LINQ cannot help you because your data is not enumerable.
For the sake of completeness, here is a LINQ solution (but it's less performant and less readable than Ginosaji's solution!)
public class Range
{
public int Min { get; set; }
public int Max { get; set; }
public static bool IsContained(Range super, Range sub)
{
return super.Min <= sub.Min
&& super.Max >= sub.Max;
}
}
public class Itemi
{
public Itemi()
{
properties = new Range[25];
for (int i = 0; i < properties.Length; i++)
{
properties[i] = new Range();
}
}
private Range[] properties;
public IEnumerable<Range> Properties { get { return properties; } }
public static bool IsContained(Itemi super, Itemi sub)
{
return super.properties
.Zip(sub.properties, (first, second) => Tuple.Create(first, second))
.All((entry) => Range.IsContained(entry.Item1, entry.Item2));
}
public Range Prop1
{
get { return properties[0]; }
set { properties[0] = value; }
}
public Range Prop2
{
get { return properties[1]; }
set { properties[1] = value; }
}
// ...
}
I have the following class objects:
public class VacancyCategory
{
public int ID { get; set; }
public string Text { get; set; }
public IList<VacancySubCategory> SubCategories { get; set; }
}
public class VacancySubCategory
{
public int ID { get; set; }
public string Text { get; set; }
public VacancyCategory Category { get; set; }
public IList<Vacancy> Vacancies { get; set; }
}
public class Vacancy : IBusinessObject
{
public int ID { get; set; }
public string Title { get; set; }
public VacancySubCategory SubCategory { get; set; }
public string Body { get; set; }
public VacancyWorkType WorkType { get; set; }
public string Salary { get; set; }
public DateTime? AppsClosingDate { get; set; }
public bool Active { get; set; }
}
...so in a test repository im creating test data like so:
private IList<VacancyCategory> GetVacancyCategoriesWithAllChildCollections()
{
IList<VacancyCategory> vacancyCategories = new List<VacancyCategory>();
int cCounter = 0;
int scCounter = 0;
int vCounter = 0;
for (int i = 1; i <= 3; i++)
{
VacancyCategory vc = new VacancyCategory();
vc.ID = ++cCounter;
vc.Text = "VacancyCategory" + i.ToString();
for (int j = 1; j <= 3; j++)
{
VacancySubCategory vsc = new VacancySubCategory();
vsc.ID = ++scCounter;
vsc.Text = "VacancySubCategory" + scCounter.ToString();
vsc.Category = vc;
for (int k = 1; k <= 2; k++)
{
Vacancy v = new Vacancy();
v.ID = ++vCounter;
v.Title = "Vacancy" + vCounter.ToString();
v.Body = "VacancyBody" + vCounter.ToString();
v.Active = vCounter >= 16 ? false : true;
v.WorkType = this._workTypes.Single(wt => wt.ID == k);
v.Salary = vCounter <= 7 ? "SR " + (vCounter * 1000).ToString() : "";
v.AppsClosingDate = (vCounter >= 3 & vCounter <= 13) ? (new DateTime(2009, 3, vCounter)) : (DateTime?)null;
v.SubCategory = vsc;
if (vsc.Vacancies == null)
vsc.Vacancies = new List<Vacancy>();
vsc.Vacancies.Add(v);
}
if (vc.SubCategories == null)
vc.SubCategories = new List<VacancySubCategory>();
vc.SubCategories.Add(vsc);
}
vacancyCategories.Add(vc);
}
return vacancyCategories;
}
..so now i have some good test data. the object tree / chained objects are important to me.
so i'd like to return the individual object collections from this tree when desired. for example, if i wanted the whole tree, i can just return the VacancyCategory list with all the child objects - great. but now i want to return just the VacancySubCaregory items (all 9 of them). this would be my public method to the test repository:
public IQueryable<VacancySubCategory> GetVacancySubCategories()
{
throw new NotImplementedException("write gen code");
}
.. obviously without the exception. i have a member field called _categories that contains the results from the GetVacancyCategoriesWithAllChildCollections method. so i've been trying stuff like
this._categories.Select( ......
..but i cant seem to return a list of VacancySubCategory objects. i seem to always be selecting the root collection (ie. a result set of VacancyCategory objects). What am i doing wrong? im sure its simple... but its driving me nuts!
EDIT
thanx matt.
your suggestion led me to this:
public IQueryable<VacancySubCategory> GetVacancySubCategories()
{
return this._categories.SelectMany(c => c.SubCategories).AsQueryable<VacancySubCategory>();
}
..which works great. you're a champ
Try:
return this._categories.SelectMany(c => c.SubCategories);
This should work.
var query = from vc in GetVacancyCategoriesWithAllChildCollections()
from vcs in vc.SubCategories
select vcs