Item is not being remove from list - c#

Item is not being remove from list
here is my code:
public interface IEmpConnection
{
int SegId { get; set; }
}
public class EmpConnection : IEmpConnection
{
private int segid;
public int SegId
{
get
{
return segid;
}
set
{
segid = value;
}
}
}
public class CustomerConnection : EmpConnection, ICustomerConnection
{
private int _id;
public int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
}
public interface ICustomerConnection
{
int Id { get; set; }
}
public class CustConn : CustomerConnection
{
private ObservableCollection<CustomerConnection> _airSeg;
public CustConn()
{
_airSeg = new System.Collections.ObjectModel.ObservableCollection<CustomerConnection>();
_airSeg.Add(new AirSegmentConnection { Id = 1, SegId = 2 });
_airSeg.Add(new AirSegmentConnection { Id = 1, SegId = 3 });
}
private bool isDeleted;
public bool IsDeleted
{
get { return isDeleted; }
set { isDeleted = value; }
}
private List<IEmpConnection> _connection;
public List<IEmpConnection> Connections
{
get
{
var s = new AirSegmentConnection();
var k = s as ISegmentConnection;
if (IsDeleted)
{
_airSeg.RemoveAt(1);
}
return _connection = _airSeg.ToList().Cast<ISegmentConnection>().ToList();
//return _airSeg.ToList().Cast<ISegmentConnection>().ToList();
}
set
{
_connection = value;
//_airSeg = new System.Collections.ObjectModel.ObservableCollection<ISegmentConnection>(value.ToList()) ;
}
}
private ObservableCollection<CustomerConnection> airConnection;
public ObservableCollection<CustomerConnection> AirConnection
{
get { return _airSeg; }
set { _airSeg = value; }
}
}
on main
button click item is not being removed. please suggest me where i am doing wrong.
CustConn a = new CustConn();
if (a.Connections.Count > 0)
{
a.Connections = new List<IEmpConnection>();
a.Connections.RemoveAt(1);// this item is not being removed.
}
please suggest i am doing worng in this code.
Thanks
Amit

It seems that you are replacing the list of connections before you remove the connection.
Since you have this marked as WPF, I'm going to assume that at some point you were able to remove the item from the List, but it still appeared on the screen. Try this:
if (a.Connections.Count > 0)
{
var newList = new List<IEmpConnection>(a.Connections);
a.Connections.RemoveAt(1);
a.Connections = newList;
}
Alternatively, you may be able to use an ObservableCollection<IEmpConnection>. This is a special collection that raises events when the collection changes. Then you would simple remove the object, and the screen would update.

You're creating a new, empty list and then trying to remove an element at position 1. In fact you've just overwritten your original list.
if (a.Connections.Count > 0)
{
/// REMOVE THIS LINE a.Connections = new List<IEmpConnection>();
a.Connections.RemoveAt(1);// this item is not being removed.
}
The line I've commented out it creating a new list and overwriting a.Connections immediately before you try to remove the item. This is what's causing the code to fail.

Related

C# Add two listviews that connected to eachother

I have two listview the other one is consider as details for the first one, however the number of the row in the second listview determined by the first listview. The first listview has 3 columns one the first two are strings and the third one is the number of rows for the second listview, how can i add both of the listview? it sounds like 2D array but in listview.
I can not find a way to link two listviews.
here is the class for both lists:
public class LvwInfo
{
private string _command = "";
private string _saperator = ",";
//
private int _noSeg=0;
public int _NoSeg { get { return _noSeg; } set { _noSeg = value;} }
public string _Command { get { return _command; } set { _command = value; } }
public string _Saperator { get { return _saperator; } set { _saperator = value; } }
public LvwInfo(string cmd,string sp,int Noseg)
{
_command = cmd;
_saperator = sp;
_noSeg = Noseg;
}
}
public class LvwInfoSeg
{
private string _segm = "";
private string _variable = "";
private string _partly = "";
public string _Segm { get { return _segm; } set { _segm = value; } }
public string _Variable { get { return _variable; } set { _variable = value; } }
public string _Partly { get { return _partly; } set { _partly = value; } }
public LvwInfoSeg(string seg, string var, string par)
{
_segm = seg;
_variable = var;
_Partly = par;
}
}
}
and Here how i initilize them:
private List<LvwInfo> _lvwInfo { get; set; }
private List<List<LvwInfoSeg>> _lvwInfoSeg { get; set; }
I want to know how can i Add both list and link them together?

Neo4jClient Node/Relationship Class conventions

Is there a standard naming convention for the properties/methods of a node/relationship class when working with Neo4jClient?
I'm following this link Neo4jClient - Retrieving relationship from Cypher query to create my relationship class
However, there are certain properties of my relationship which i can't get any value despite the relationship having it. While debugging my code, i realized certain properties was not retrieved from the relationship when creating the relationship object.
this is my relationship class
public class Creates
{
private string _raw;
private int _sourcePort;
private string _image;
private int _DestinationPort;
private int _eventcode;
private string _name;
private string _src_ip;
private int _src_port;
private string _dvc;
private int _signature_ID;
private string _dest_ip;
private string _computer;
private string _sourceType;
private int _recordID;
private int _processID;
private DateTime _time;
private int _dest_port;
public string Raw { get { return _raw; } set { _raw = value; } }
public int SourcePort { get { return _sourcePort; } set { _sourcePort = value; } }
public string Image { get { return _image; } set { _image = value; } }
public int DestinationPort { get { return _DestinationPort; } set { _DestinationPort = value; } }
public int Eventcode { get { return _eventcode; } set { _eventcode = value; } }
public string Name { get { return _name; } set { _name = value; } }
public string Src_ip { get { return _src_ip; } set { _src_ip = value; } }
public int Src_port { get { return _src_port; } set { _src_port = value; } }
public string DVC { get { return _dvc; } set { _dvc = value; } }
public int Signature_ID { get { return _signature_ID; } set { _signature_ID = value; } }
public string Dest_ip { get { return _dest_ip; } set { _dest_ip = value; } }
public string Computer { get { return _computer; } set { _computer = value; } }
public string SourceType { get { return _sourceType; } set { _sourceType = value; } }
public int RecordID { get { return _recordID; } set { _recordID = value; } }
public int ProcessID { get { return _processID; } set { _processID = value; } }
public DateTime Indextime { get { return _time; } set { _time = value; } }
public int Dest_port { get { return _dest_port; } set { _dest_port = value; } }
}
This is another class
public class ProcessConnectedIP
{
public Neo4jClient.RelationshipInstance<Pivot> bindto { get; set; }
public Neo4jClient.Node<LogEvent> bindip { get; set; }
public Neo4jClient.RelationshipInstance<Pivot> connectto { get; set; }
public Neo4jClient.Node<LogEvent> connectip { get; set; }
}
This is my neo4jclient query to get the relationship object
public IEnumerable<ProcessConnectedIP> GetConnectedIPs(string nodeName)
{
try
{
var result =
this.client.Cypher.Match("(sourceNode:Process{name:{nameParam}})-[b:Bind_IP]->(bind:IP_Address)-[c:Connect_IP]->(connect:IP_Address)")
.WithParam("nameParam", nodeName)
.Where("b.dest_ip = c.dest_ip")
.AndWhere("c.Image=~{imageParam}")
.WithParam("imageParam", $".*" + nodeName + ".*")
.Return((b, bind, c, connect) => new ProcessConnectedIP
{
bindto = b.As<RelationshipInstance<Creates>>(),
bindip = bind.As<Node<LogEvent>>(),
connectto = c.As<RelationshipInstance<Creates>>(),
connectip = connect.As<Node<LogEvent>>()
})
.Results;
return result;
}catch(Exception ex)
{
Console.WriteLine("GetConnectedIPs: Error Msg: " + ex.Message);
return null;
}
}
This is the method to read the results
public void MyMethod(string name)
{
IEnumerable<ProcessConnectedIP> result = clientDAL.GetConnectedIPs(name);
if(result != null)
{
var results = result.ToList();
Console.WriteLine(results.Count());
foreach (ProcessConnectedIP item in results)
{
Console.WriteLine(item.Data.Src_ip);
Console.WriteLine(item.bindto.StartNodeReference.Id);
Console.WriteLine(item.bindto.EndNodeReference.Id);
Console.WriteLine(item.connectto.StartNodeReference.Id);
Console.WriteLine(item.connectto.EndNodeReference.Id);
Node<LogEvent> ans = item.bindip;
LogEvent log = ans.Data;
Console.WriteLine(log.Name);
Node<LogEvent> ans1 = item.connectip;
LogEvent log1 = ans1.Data;
Console.WriteLine(log1.Name);
}
}
}
Somehow, i'm only able to populate the relationship object with src_ip/src_port/dest_ip/dest_port values. the rest are empty.
Is there any possible reason why? I've played with upper/lower cases on the properties names but it does not seem to work.
This is the section of the graph im working with
This is the relationship properties sample:
_raw: Some XML dataSourcePort: 49767Image: C:\Windows\explorer.exeDestinationPort: 443EventCode: 3Name: Bind
IPsrc_ip: 172.10.10.104dvc: COMPUTER-NAMEsrc_port:
49767signature_id: 3dest_ip: 172.10.10.11Computer:
COMPUTRE-NAME_sourcetype:
XmlWinEventLog:Microsoft-Windows-Sysmon/OperationalRecordID:
13405621ProcessId: 7184_time: 2017-08-28T15:15:39+08:00dest_port: 443
I'm not entirely sure how your Creates class is ever populated, in particular those fields - as your Src_port property doesn't match the src_port in the sample you provided (case wise).
I think it's probably best to go back to a super simple version. Neo4jClient will map your properties to the properties in the Relationship as long as they have the same name (and it is case-sensitive).
So start with a new Creates class (and use auto properties - it'll make your life a lot easier!)
public class Creates
{
public string Computer { get; set; }
}
Run your query with that and see if you get a result, then keep on adding properties that match the name and type you expect to get back (int, string etc)
It seems that i have to give neo4j node/relationship property names in lowercase and without special characters at the start of the property name, in order for the above codes to work.
The graph was not created by me at the start thus i had to work on it with what was given. I had to get the developer who created the graph to create the nodes with lowercases in order for the above to work.

linq update not working

I have tried a lot but all in vain.
I have written a LINQ code but not able to save changes in database.
It is giving no error neither it is updating record.
class Program
{
[Table(Name = "mainframe_replication")]
public class mainframe_replication
{
private string _REPL_GUID;
[Column(IsPrimaryKey = true, Storage = "_REPL_GUID")]
public string REPL_GUID
{
get { return this._REPL_GUID; }
set { this._REPL_GUID = value; }
}
private string _REPL_TYPE;
[Column(Storage = "_REPL_TYPE")]
public string REPL_TYPE
{
get { return this._REPL_TYPE; }
set { this._REPL_TYPE = value; }
}
private string _RPT_ID;
[Column(Storage = "_RPT_ID")]
public string RPT_ID
{
get { return this._RPT_ID; }
set { this._RPT_ID = value; }
}
private string _RPT_VERS;
[Column(Storage = "_RPT_VERS")]
public string RPT_VERS
{
get { return this._RPT_VERS; }
set { this._RPT_VERS = value; }
}
private string _RPT_BYTES;
[Column(Storage = "_RPT_BYTES")]
public string RPT_BYTES
{
get { return this._RPT_BYTES; }
set { this._RPT_BYTES = value; }
}
private string _REPL_DTM;
[Column(Storage = "_REPL_DTM")]
public string REPL_DTM
{
get { return this._REPL_DTM; }
set { this._REPL_DTM = value; }
}
private string _NOTIF_ID;
[Column(Storage = "_NOTIF_ID")]
public string NOTIF_ID
{
get { return this._NOTIF_ID; }
set { this._NOTIF_ID = value; }
}
}
public class MyPoco
{
public string ReportId { get; set; }
public string Reportversion { get; set; }
public string ReportBytes { get; set; }
public string ReportDate { get; set; }
public string NotifId { get; set; }
public string RecipAdd { get; set; }
}
public static string loglocation;
static void Main(string[] args)
{
try
{
using (DataClasses1DataContext db = new DataClasses1DataContext())
{
Table<NOTIF_RECIP> NOTIF_RECIP_alias = db.GetTable<NOTIF_RECIP>();
Table<NOTIF_SCHED> NOTIF_SCHED_alias = db.GetTable<NOTIF_SCHED>();
Table<mainframe_replication> mainframe_replication_alias = db.GetTable<mainframe_replication>();
var ids = NOTIF_SCHED_alias.Select(x => x.NOTIF_RPT_ID).ToArray();
foreach (string notif_sched_data in ids)
{
var repljoinmf = mainframe_replication_alias
.Join(NOTIF_RECIP_alias, mfr => mfr.RPT_ID, nr => nr.NOTIF_RECIP_ID, (mfr, nr)
=> new MyPoco { ReportId = mfr.RPT_ID, Reportversion = mfr.RPT_VERS, ReportBytes = mfr.RPT_BYTES.ToString(), ReportDate = mfr.REPL_DTM.ToString(), NotifId = mfr.NOTIF_ID, RecipAdd = nr.NOTIF_RECIP_ADDR });
foreach (var repljoinmf_data in repljoinmf)
{
repljoinmf_data.NotifId = "abc";
//DO STUFF
// repljoinmf_data.NotifId = "Changedxyz";
}
db.SubmitChanges();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
It is not giving any error while submitting changes.
What I need to change?
Any suggestion will be helpful.
If you want to save your changes back to the original data source, you need to be working with the actual entities instead of projections of those entities. Since you are joining two tables, one option is to put those instances into an anonymous type and update them:
foreach (string notif_sched_data in ids)
{
var repljoinmf = mainframe_replication_alias
.Join(NOTIF_RECIP_alias,
mfr => mfr.RPT_ID,
nr => nr.NOTIF_RECIP_ID,
(mfr, nr) => new {mfr, nr});
foreach (var repljoinmf_data in repljoinmf)
{
//DO STUFF
repljoinmf_data.mfr.NotifId = "Changedxyz";
}
db.SubmitChanges();
In your previous question you were told that anonymous types cannot be uptated, but in this case you're modifying instances that are referenced by the anonymous type. So you're not updating the anonymous type itself, just the objects that the anonymous type references.
You are modifying the property of your MyPoco object. This is just a representation of your table. That's why the database is not updated.
You can send your MyPoco to your client. It will perform some changes. Then you can recreate the entity and copy the properties from the Poco object. Then, you need to attach the modified entity to your table and then save the changes.
If you modify directly the entity, there is no need to attach, since it will have kept the links to the database (assuming you do that with the same Databasecontext).

WPF ViewModel Issue

I have a recursive viewmodel that uses for a treeview. The problem is everytime I delete a Group Item or Entry Item. The treeview cannot reflect the change immediately until I force it by treeview.Items.Refresh(). My guess is the ObservableCollection Items did get notified. Could you one please point me out. Thanks.
ViewModel
public class Group:ViewModelBase
{
private int _key;
public int Key
{
get { return _key; }
set { _key = value; OnPropertyChanged("Key"); }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; OnPropertyChanged("Name"); }
}
private bool _isexpanded;
public bool IsExpanded
{
get { return _isexpanded; }
set { _isexpanded = value; OnPropertyChanged("IsExpanded"); }
}
private int _order;
public int Order
{
get { return _order; }
set { _order = value; OnPropertyChanged("Order"); }
}
private int _grouporder;
public int GroupOrder
{
get { return _grouporder; }
set { _grouporder = value; OnPropertyChanged("GroupOrder"); }
}
private string _error;
public string Error
{
get { return _error; }
set { _error = value; OnPropertyChanged("Error"); }
}
private ObservableCollection<Group> _subgroups;
public ObservableCollection<Group> SubGroups
{
get { return _subgroups; }
set { _subgroups = value;
OnPropertyChanged("SubGroups");
OnPropertyChanged("Entries");
}
}
private ObservableCollection<Entry> _entries;
public ObservableCollection<Entry> Entries
{
get { return _entries; }
set { _entries = value;
OnPropertyChanged("SubGroups");
OnPropertyChanged("Entries"); }
}
public ObservableCollection<object> Items
{
get
{
ObservableCollection<object> childNodes = new ObservableCollection<object>();
foreach (var entry in this.Entries)
childNodes.Add(entry);
foreach (var group in this.SubGroups)
childNodes.Add(group);
return childNodes;
}
}
}
Binding:
public MainWindow()
{
InitializeComponent();
treeview.ItemsSource = Groups;
this.DataContext = this;
}
The issue is that you break the link with the original collections.
So I can imagine two solutions:
1) Register for changes on Entries and SubGroups and apply them back to Items:
private ObservableCollection<object> items;
public ObservableCollection<object> Items
{
get
{
if (items == null)
{
items = new ObservableCollection<object>();
foreach (var entry in this.Entries)
items.Add(entry);
foreach (var group in this.SubGroups)
items.Add(group);
this.Entries.CollectionChanged += (s, a) =>
{
if (/*add some stuff*/)
{
items.Add(/*some stuff*/)
}
else if (/*remove some stuff*/)
{
items.Remove(...)
}
};
this.SubGroups.CollectionChanged += ...
return items;
}
}
}
And you would reset items by setting it to null each time you set Entries or SubGroups so that it is regenerated next time, taking into account new Entries and SubGroups.
2) Try with a CompositeCollection:
new CompositeCollection
{
new CollectionContainer { Collection = this.Entries },
new CollectionContainer { Collection = this.SubGroups }
};
Not sure for this second solution but it would be quite elegant, worth a try...

Why Datetime property value changes when I retrieve it from a List?

I have a class called Job and that encapsulates an object of another class called SelectedItem which has a DateTime property called ItemDate;
class Job
{
private SelectedItem m_SelectedItem = null;
public Job() { }
public SelectedItem Item
{
get { return m_SelectedItem; }
set { m_SelectedItem = value; }
}
}
class SelectedItem
{
DateTime m_ItemDate = default(DateTime);
public SelectedItem() { }
public DateTime ItemDate
{
get
{
return (m_ItemDate == default(DateTime))
? DateTime.Now
: m_ItemDate;
}
set { m_ItemDate = value; }
}
}
Then I have a JobManager class that has a List of Job: JobQueue;
class JobManager
{
private ICollection<Job> m_JobQueue = null;
public JobManager()
{
m_JobQueue = new List<Job>();
}
public ReadOnlyCollection<Job> JobQueue
{
get { return m_JobQueue.ToList().AsReadOnly(); }
}
private void CreateJob(SelectedItem item)
{
Job newJob = new Job();
newJob.Item = item;
m_JobQueue.Add(newJob);
}
public IList<Job> GetJobsFromQueue(int quantity, ICollection<Job> ignoreList)
{
return (from job in JobQueue
select job).Skip(ignoreList.Count).Take(quantity).ToList();
}
public void CreateJobQueue(ICollection<SelectedItem> jobData)
{
for (long daysCount = 100; daysCount >= 1; daysCount--)
{
SelectedItem item = GetRandomItem(jobData.ToList()); //This just returns a random item from the specified jobData.
item.ItemDate = DateTime.Today.AddDays((daysCount - 1));
CreateJob(item);
}
}
}
Now the issue is when I call the CreateJobQueue() from the main routine, the ItemDate is correctly inserted as in the future depending upon the daysCount value and populates the JobQueue perfectly.
But when try to retrieve a set of Jobs by calling in GetJobsFromQueue() method, the ItemDate values in the whole of JobQueue is messed up (i.e. different to what has been inserted).
Can anyone have a clue?

Categories

Resources