I have a List which is getting filled correct, however after its being filled I want to remove all keys with a null value on a simple way, if exists, something like:
posts.RemoveAll(item => item == null);
I have multiple posts in this List<>
For me the problem is to find the Key/Value to access as in a Dictionary.
Does somebody know the way to do this?
UPDATE:
my class looks like this:
public class iPost
{
public int id { get; set; }
public int post_origin_post_id { get; set; }
public int pid { get; set; }
public int container_type_id { get; set; }
public int post_member_id { get; set; }
public int post_toGroup_id { get; set; }
public string title { get; set; }
public string comment { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string post_ip { get; set; }
public bool canShared { get; set; }
public bool isShared { get; set; }
public int share_type_id { get; set; }
public int views { get; set; }
Where is your key? A c#-List has no keys.
Do you mean somethong like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoApp
{
class Program
{
static void Main(string[] args)
{
List<DemoClass> list = new List<DemoClass>();
list.Add(new DemoClass() { Key = "A1" });
list.Add(new DemoClass() { Key = "A2" });
list.Add(new DemoClass() { Key = "A3" });
// Print
foreach (var it in list) { Console.WriteLine(it.Key); }
Console.WriteLine();
// set one key to null
list[1].Key = null;
list.RemoveAll(Item => Item.IsToDelete());
// Print
foreach (var it in list) { Console.WriteLine(it.Key); }
Console.WriteLine("As you can see, A2 is missing");
Console.ReadLine();
}
}
class DemoClass
{
public string Key
{
get;
set;
}
public object SomeValue
{
get;
set;
}
public bool IsToDelete()
{
if(Key == null || SomeValue == null)
{
return true;
}
return false;
}
}
}
Edit
You can write an own attribute, which is placed over all members who are not allwed to be null. Than you can write an class which read this information and than decise if your instance ha to be removed from the list. Absract example:
[ProofForNull]
public string Key
{
get;
set;
}
[ProofForNull]
public string Value1
{
get;
set;
}
[ProofForNull]
public string Value2
{
get;
set;
}
Than you change IsToDelete(). It has to get all member via reflection and proof if they have the [ProofForNull] attribute. If one member with the attribute is null, IsToDelete() returns true.
With this method you have one initial work to do, but the implementation for your classes is much easier.
Related
I have a Model that is filled with 20 Properties, for instance such as
public class SensorModel
{
public string Trigger1 { get; set; }
public string PathDoor1 { get; set; }
public string PathDoor2 { get; set; }
public string PathTrigger1 { get; set; }
public string PathTrigger2 { get; set; }
public string PathTrigger3 { get; set; }
public string PathTrigger4 { get; set; }
public string PathTrigger5 { get; set; }
public string PathTrigger6 { get; set; }
public string PathTrigger7 { get; set; }
public string PathTrigger8 { get; set; }
}
After declaring and setting their properties by doing such,
SensorModel sensorsData = new SensorModel();
How can I access sensorsData's properties using a loop?
Because I would like to logs all the data into a txt along with DateTime, I find manually accessing is a waste of time.
Is there any way to automate, for instance, using a loop and accessing it one by one?
You can use reflection to achieve your goal:
var model = new SensorModel() {
PathDoor1 = "Foo",
PathDoor2 = "Foo2",
PathTrigger1 = "Value of PT1",
PathTrigger2 = "Value of PT2",
};
foreach(var value in model.GetTriggerValues()) {
Console.WriteLine(value);
}
public class SensorModel
{
public string Trigger1 { get; set; }
public string PathDoor1 { get; set; }
public string PathDoor2 { get; set; }
public string PathTrigger1 { get; set; }
public string PathTrigger2 { get; set; }
/* ... */
public IEnumerable<string> GetTriggerValues() {
foreach(var prop in this.GetType().GetProperties().Where(x => x.Name.StartsWith("PathTrigger"))) {
yield return (string)prop.GetValue(this, null);
}
}
}
This example filters your properties by name, if you want or need a different result set, amend or remove the where clause.
You can use reflection to achieve this:
var obj = new SensorModel();
// ...
// Get all the properties of your class
var props = typeof(SensorModel).GetProperties();
foreach (var prop in props)
{
// Get the "Get" method and invoke it
var propValue = prop.GetGetMethod()?.Invoke(obj, null);
// Do something with the value
Console.Out.WriteLine("propValue = {0}", propValue);
}
I'm actually using EF and very new to it. I have a EDMX-Model from Database. I have to get a List of Entities, but in this Table is Binary-Column. I want my Object-List from that Table without the Binary Data.
My Entity-Objekt looks like:
public partial class bons
{
public int id { get; set; }
public System.DateTime zeitstempel { get; set; }
public byte[] bonBinaer { get; set; }
public Nullable<int> tisch_nr { get; set; }
public bool abgearbeitet { get; set; }
public int kunde_id { get; set; }
public string hash { get; set; }
public string dateiname { get; set; }
public string tisch_name { get; set; }
public int gang { get; set; }
public decimal brutto { get; set; }
public Nullable<System.DateTime> zeitstempelAbgearbeitet { get; set; }
public Nullable<int> positionenAnzahl { get; set; }
public bool manuell { get; set; }
}
And I#m getting the List like:
internal static List<bons> holeBongListeNichtAbgearbeitetRestaurant(int kunde_id)
{
List<bons> rückgabe = new List<bons>();
using (bonsEntities context = new bonsEntities())
{
rückgabe = context.bons.Where(x => x.kunde_id == kunde_id && x.abgearbeitet == false).OrderBy(x => x.zeitstempel).ToList();
}
return rückgabe;
}
Can someone help how to get the List without the 'byte[] bonBinaer'?
Hi you can use autoMapper with EF extension
like: https://docs.automapper.org/en/stable/Queryable-Extensions.html
or if you prefer you can do by yourself with a Select() method like:
using (bonsEntities context = new bonsEntities())
{
rückgabe = context.bons.Where(x => x.kunde_id == kunde_id && x.abgearbeitet == false).OrderBy(x => x.zeitstempel).Select(xx=> new {
id = xx.id
//and all you props
}).ToList().Select(yy=> new bons{
id=yy.id
//and back with other props
}).ToList();
}
I have two objects (A,B) of same class type (PPLWebOperatorGridList). I need update the A.OldValue with B.Value.
I have tried by adding the guid property and update it in the constructor as shown below. But these object list may repeat same value:
public PPLWebOperatorGridList()
{
this.guid = this.FieldName+this.TagName+
this.Length+this.Encoder+this.Value;
}
public string guid { get; set; }
I have tried as below. I know there are bugs in it but consider the idea in it.
private List<PPLWebOperatorGridList> UpddateOldValues(List<PPLWebOperatorGridList> customeTlvList, List<PPLWebOperatorGridList> customeTlvList2)
{
foreach (var list in customeTlvList)
{
foreach (var list1 in customeTlvList2)
{
if (list.guid == list1.guid)
{
list.OldValue = list1.Value;
if (list.children.Count > 0)
UpddateOldValues(list.children.ToList(), list1.children.ToList());
}
}
}
return customeTlvList;
}
The guid property may be same for some in the list.
class PPLWebOperatorGridList
{
public bool expanded { get; set; }
public string FieldName { get; set; }
public string TagName { get; set; }
public string Length { get; set; }
public string Encoder { get; set; }
public string Value { get; set; }
public List<PPLWebOperatorGridList> children { get; set; }
public string OldValue { get; set; }
}
I need to loop through based on index and update the A.OldValue with B.Value. I am not very familiar with linq, so please suggest a solution.
Code without AutoMapper:
List<CountryDM> countryDMList = _countryRepo.GetCountry();
List<CountryVM> countryVMList = new List<CountryVM>();
foreach (CountryDM countryDM in countryDMList)
{
countryVMList.Add(CountryVM.ToViewModel(countryDM));
}
return countryVMList;
I used AutoMapper for the above task. But it returns a NULL list. Please refer the below code:
List<CountryDM> countryDMList = _countryRepo.GetCountry();
Mapper.CreateMap<List<CountryDM>, List<CountryVM>>();
List<CountryVM> countryVMList = new List<CountryVM>();
return Mapper.Map<List<CountryVM>>(countryDMList);
public class CountryDM
{
public int ID { get; set; }
public string CountryCode { get; set; }
public string Description { get; set; }
}
public class CountryVM
{
public int ID { get; set; }
public string CountryCode { get; set; }
public string Description { get; set; }
}
You don't need to define a mapping between lists, just between objects, AutoMapper will know how to extrapolate that:
Mapper.CreateMap<CountryDM, CountryVM>();
the rest stays the same
I have a List<Ap21Stock> where by Ap21Stock looks like this:
public class Ap21Stock
{
public Ap21Product Product { get; set; }
public int FreeStock { get; set; }
}
with Ap21Product looking like:
public class Ap21Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string StyleCode { get; set; }
public string ColourCode { get; set; }
public string SizeCode { get; set; }
}
I also have another List<Product> that looks like this:
public class Product
{
public string SellerSku { get; set; }
public string ShopSku { get; set; }
public int Quantity { get; set; }
public int FulfillmentByNonSellable { get; set; }
}
I need to produce a separate list:
- The key of the first list is the concatenation of StyleCode, ColourCode and SizeCode separated by a period (.). This matches up to the SellerSku property in the second list.
- Only output values where FreeStock != Quantity.
Basically, I have 2 stock lists from separate system and need to lookup the differences, however both are keyed differently (first is keyed in separate fields whereas the second is one field keyed in a single string).
How would I do this?
I tried the below but it doesn't feel right (and possibly wrong and inefficient as I am getting zero results in my last dictionary which I haven't yet looked into):
Dictionary<string, int> apparel21StocksKeyed = new Dictionary<string, int>();
foreach ( var product in apparel21Stocks )
{
apparel21StocksKeyed.Add(string.Concat(product.StyleCode, ".", product.ColourCode, ".", product.SizeCode, "."), product.FreeStock);
}
//loop over the products, producing a list to send back to The Iconic
Dictionary<string, int> productsToUpdate = new Dictionary<string, int>();
foreach (var stock in iconicStocks)
{
if (apparel21StocksKeyed.ContainsKey(stock.SellerSku) && apparel21StocksKeyed[stock.SellerSku] != stock.Quantity)
{
productsToUpdate.Add(stock.SellerSku, apparel21StocksKeyed[stock.SellerSku]);
}
}
Try this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Ap21Stock> stock = new List<Ap21Stock>();
List<Product> products = new List<Product>();
var results = from s in stock
join p in products on string.Join(".", new string[] { s.ProductId.ToString(), s.StyleCode, s.ColourCode }) equals p.SellerSku
where s.FreeStock != p.Quantity
select new { id = s.ProductId, sku = p.SellerSku };
}
}
public class Ap21Stock
{
public int ProductId { get; set; }
public string StyleCode { get; set; }
public string ColourCode { get; set; }
public string SizeCode { get; set; }
public int FreeStock { get; set; }
}
public class Product
{
public string SellerSku { get; set; }
public string ShopSku { get; set; }
public int Quantity { get; set; }
public int FulfillmentByNonSellable { get; set; }
}
}