I have make one list str.
List<string> str = new List<string>();
str.AddRange(new string[] { "ahmedabad", "surat", "vadodara", "rajkot", "bhavnagar", "jamnagar", "gandhidham", "gandhinagar" });
I have create one class
class MatchAddress
{
public string Name { get; set; }
public string Taluka { get; set; }
public string District { get; set; }
public string Pincode { get; set; }
public float Rank { get; set; }
}
List<MatchAddress> lm;
List<MatchAddress> lmmatch.
fill the lm list.
lm.Add(new MatchAddress() { District = "vadodara", Taluka = "vadodara", Rank = 1f, Name = "vadodara" });
lm.Add(new MatchAddress() { District = "gandhinagar", Taluka = "gandhinagar", Rank = 1f, Name = "vadodara" });
foreach (MatchAddress ma in lm)
{
string pincode ="";
// what logic i write in below then only we got the vadodara because that name are in both taluka and name then only set nulll if any one differ then not set null.
if (str.Contains(ma.Name) && str.Contains(ma.Taluka.ToLower()))
{
pincode = null;
}
lmmatch.Add(new MatchAddress() { Name = ma.Name, District = ma.District, Pincode = (pincode == null)? null : ma.Pincode , Rank = rank, Taluka = ma.Taluka });
}
so in this lmmatch list for both the MatchAddress object set pincode null but i wan't null for vadodara only and that have same taluka and village name.
if the taluka and name are different then not set pincode null.
reason behind this is where in lm list where pincode and taluka both have vadodara then only set the pincode null .
but this code that i have write also set pincode null for name vadodara where taluka not same they have gandhinagar.
so after rus this code i have got two object and they both have set pincode null but i wan't to only for vadodara where they have both name and taluka same.
so any idea how i can solve this problem ?
You Need to change only in your code
if (str.Contains(ma.Name) && ma.Name.Equals(ma.Taluka))
You Just Need to comare ma.Name = ma.taluka simple
Change your if as below
if (str.Contains(ma.Name) && ma.Name.ToLower().Equals(ma.Taluka.ToLower()))
Related
I have an object that is supposed to describe some customer with these values:
{
public class CustomerDescription
{
public string FirstName { get; set; } = "";
public string LastName { get; set; } = "";
public string StreetName { get; set; } = "";
public string HouseNumber { get; set; } = "";
public string PostalPlace { get; set; } = "";
public string PostCode { get; set; } = "";
public string CountryCode { get; set; } = "";
public string EMail { get; set; } = "";
public string PhoneNumber { get; set; } = "";
}
}
These values are retrieved from a database of customers and will be used to create an XML file which will be sent via SOAP to another database. However, not all these values are always present. For example one customer may not have the country code in the database. In this case I would like to not include this value at all in the XML file I am sending. Here is an example of how I create the XML elements:
new XElement("address",
new XElement("address1", CustomerDescription.StreetName + " " + cabCustomerDescription.HouseNumber),
new XElement("postalCode", cUstomerDescription.PostCode),
new XElement("city", customerDescription.PostalPlace),
new XElement("countryCode", customerDescription.CountryCode))
)
This is done for all the properties in CustomerDescription. My question is, how can I do this so that if a value is not present in the database, then this value is not included in the XML file? I would not like it to be empty, like <countryCode></countryCode>, but rather not present at all.
You just need to use ternary operator if value is null from database dont include in x element.
Ex:Explicitely i did StreetName null an dcheck if null then dont add in xml file.
CustomerDescription t = new CustomerDescription();
t.StreetName = null;
var abc = new XElement("address", t.StreetName != null ? new XElement("address1", t.StreetName + " " + t.HouseNumber) : null,
new XElement("postalCode", t.PostCode),
new XElement("city", t.PostalPlace),
new XElement("countryCode", t.CountryCode));
I am attemping to read a text file in the format of
(The # at end is just the number of classes they're in, but I dont save the course name with the fac/students class)
Course Biology
Faculty Taylor Nate 0
Student Doe John 3
Student Sean Big 0
Course Art
Faculty Leasure Dan 1
The first input should be a course, followed by the faculty and students of the specific course. The Course class should contain a collection of faculty members and a collection of students.
I have been able to put each course/student/faculty into their respective class, but I am having trouble visualizing a way to add the students/faculty to the course.
My current idea putting the data into their respective classes would be to keep the current index of the course- therefore I have it saved as
courses[currentCourse++]
so when I parse the next line, (being a faculty/student) I already know what the course index should be.
using (StreamReader reader = new StreamReader(fileName))
{
while (!reader.EndOfStream)
{
lineCounter++;
line = reader.ReadLine();
string[] words = line.Split(' ');
Console.WriteLine(words[0]);
if (words[0] == "Course")
{
string nameOfCourse = words[1];
courses[currentCourse++] = new Course
{
Name = nameOfCourse
};
}
if (words[0] == "Faculty")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
faculty[currentFaculty++] = new Faculty
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
};
}
if (words[0] == "Student")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
students[currentStudent++] = new Student
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
};
}
I know the problem lies in the courses class itself- but i'm not sure the terminology to add a class to another class.
public class Course
{
public override string ToString()
{
return $"{Name}";
}
public string Name { get; set; }
}
public class Student
{
public override string ToString()
{
return $"{FirstName} {LastName} {NumOfClasses}";
}
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string NumOfClasses { get; set; } = string.Empty;
}
Thanks for reading!
You want to add a collection of Student and Faculty to the course class, correct? You can do so like this by simply adding a List<T> to your Course class and then initializing it in a constructor.
public class Course
{
public override string ToString()
{
return $"{Name}";
}
public string Name { get; set; }
public List<Student> Students { get; set; }
public List<Faculty> FacultyMems { get; set; }
public Course()
{
Students = new List<Student>();
FacultyMems = new List<Faculty>();
}
}
And in your using block, you can add each student/faculty to the course as so:
if (words[0] == "Course")
{
string nameOfCourse = words[1];
currentCourse++;
courses[currentCourse] = new Course
{
Name = nameOfCourse
};
}
if (words[0] == "Faculty")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
courses[currentCourse].FacultyMems.Add(new Faculty
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
});
}
if (words[0] == "Student")
{
string firstName = words[1];
string lastName = words[2];
string numOfClasses = words[3];
courses[currentCourse].Students.Add(new Student
{
FirstName = firstName,
LastName = lastName,
NumOfClasses = numOfClasses,
});
}
With this, each time you encounter "Course" your course list will add a new item and then you can append students/faculty/etc when those values occur.
This can be simplified even further but the concept is there for you to follow. Hope this helps.
If I'm understanding you correctly, you want your courses to have a list of faculty and students?
public class Course
{
public override string ToString()
{
return $"{Name}";
}
public string Name { get; set; }
public List<Student> Students { get; set; }
public List<Faculty> FacultyMembers {get; set;}
}
Just be sure to initialize the Lists before trying to add things to them otherwise you'll get a null ref exception.
I want to order by pincode with string empty, when I am trying to convert pincode to an integer for sorting, I am getting an error.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Pincode { get; set; }
}
List<Student> objStudentList = new List<Student>();
objStudentList.Add(new Student() { Id = 1, Name = "gopi", City="Chennai", Pincode = "600002" });
objStudentList.Add(new Student() { Id = 2, Name = "ravi", City = "Bangulor", Pincode = "600 001" });
objStudentList.Add(new Student() { Id = 3, Name = "mani", City = "Madurai", Pincode = "600 007" });
objStudentList.Add(new Student() { Id = 4, Name = "anbu", City = "Thiruchi", Pincode = "600 005" });
objStudentList.Add(new Student() { Id = 4, Name = "kumar", City = "Thiruchi", Pincode = "" });
objStudentList = objStudentList.OrderBy(a => int.Parse(Regex.Replace(a.Pincode.Replace(" ", "").Replace("\t", ""), #"\t|\n|\r", ""))).ToList();
can anyone tell me what the problem at hand is and how to fix it?
you can avoid empty strings by first filtering them out. i.e.
objStudentList = objStudentList.Where(a => !string.IsNullOrEmpty(a.Pincode))
.OrderBy(a => int.Parse(a.Pincode)).ToList();
However, if you want to keep the Student objects that have an empty string but rather replace their Pincode property with "0" you can try this:
objStudentList = objStudentList.OrderBy(a => string.IsNullOrEmpty(a.Pincode) ? int.Parse("0") : int.Parse(a.Pincode))
.ToList();
The reason is that you're trying to covert an empty string to int. If you want to get 0 as a result, you must replace "" to "0"
I'm creating an WindowsForms application that is using a list of persons with 4 parameters (ID, Name, Surname, Permissions):
public List<Osoba> ListaOsoba()
{
Osoba nr1 = new Osoba(1, "Name", "Surname", Permissions.Administrator);
Osoba nr2 = new Osoba(2, "Name2", "Surname2", Permissions.Użytkownik);
Osoba nr3 = new Osoba(3, "Name3", "Surname3", Permissions.Użytkownik);
listaOsób.Add(nr1);
listaOsób.Add(nr2);
listaOsób.Add(nr3);
return listaOsób;
}
I would like to post all those Parameters to CheckedListBox, but show only name and surname to the user. The ID and Permissions should be hidden, but they need to exist, because I want to use them later.
Every help will be appreciated.
public static bool CheckBoxListPopulate(CheckBoxList CbList, IList<T> liSource, string TextFiled, string ValueField)
{
try
{
CbList.Items.Clear();
if (liSource.Count > 0)
{
CbList.DataSource = liSource;
CbList.DataTextField = TextFiled;
CbList.DataValueField = ValueField;
CbList.DataBind();
return true;
}
else { return false; }
}
catch (Exception ex)
{ throw ex; }
finally
{
}
}
here Cb list is the control name and
List item Ilist is the list source name
Text field (should be concatination ) ="Name" + "Surname"
Value field will be Hidden it can be "1,2,3"
so only Text field will be visible to user
To bind only name and surname to checkedboxlist first store name and surname together and then try this:
NameS = "Name" + "Surname";
((ListBox)checkedListBox).DataSource = listaOsób;
((ListBox)checkedListBox).DisplayMember = "NameS";
try this, here you have to make arbitrary compound properties for display and value member like DisplayName and HiddenId and then you can easily bound with checkedlistbox.
public class Osoba
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Permissions Permission { get; set; }
public string DisplayName { get; set; }
public string HiddenId { get; set; }
public Osoba()
{ }
public Osoba(int id, string fname, string lname, Permissions p)
{
Id = id;
FirstName = fname;
LastName = lname;
Permission = p;
DisplayName = FirstName + " " + LastName;
HiddenId = Id + "_" + Permission.GetHashCode();
}
public void ListaOsoba()
{
List<Osoba> objList = new List<Osoba>();
Osoba nr1 = new Osoba(1, "Name", "Surname", Permissions.Administrator);
Osoba nr2 = new Osoba(2, "Name2", "Surname2", Permissions.Uzytkownik);
Osoba nr3 = new Osoba(3, "Name3", "Surname3", Permissions.Uzytkownik);
objList.Add(nr1);
objList.Add(nr2);
objList.Add(nr3);
((ListBox)checkedListBox1).DataSource = objList;
((ListBox)checkedListBox1).DisplayMember = "DisplayName";
((ListBox)checkedListBox1).ValueMember = "HiddenId";
MessageBox.Show(((ListBox)checkedListBox1).Text);
MessageBox.Show(((ListBox)checkedListBox1).SelectedValue.ToString());
}
}
public enum Permissions
{
Administrator,
Uzytkownik
}
I had a similar thing with SQL. I returned many columns, but only wanted one to show.
Anyway
ArrayList arr = new ArrayList();
foreach (object o in ListaOsoba)
{
arr.Items.Add(o[1].ToString()+" "+o[2].ToString());
}
foreach (var item in arr)
{
chkNames.Items.Add(arr.ToString()); //chkNames is your CheckListBox
}
Then later when querying which ID and such goes where, loop through you original list, and see who was ticked based on the name and surname combo, find the ID related to that person and you should be sorted
I have the following:
IEnumerable<Job> jobs
jobs = from t in table.GetAll()
select new Job
{
Key1 = t.Key1,
Key2 = t.Key2,
Title = t.Title,
Status = t.Status,
Type = t.Type
};
The Status and Type fields are keys and the value of these keys is stored in the following:
refStatus = from t in ref.GetAll()
select new Ref
{
Key1 = "Status"
Key2 = t.Key2, // key that links to Status in the Jobs
Title = t.Title // Text value of the key
}
refType = from t in ref.GetAll()
select new Ref
{
Key1 = "Type"
Key2 = t.Key2, // key that links to Type in the Jobs
Title = t.Title // Text value of the key
}
Is there a way in LINQ that I can link the refStatus and refType tables to the first table and then have my jobs report show the text value of the Status and the Type instead of the key only?
public abstract class Job
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Title { get; set; }
public string Status { get; set; }
public string Type { get; set; }
}
public abstract class Ref
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Title { get; set; }
}
Try the following:
var results =
from j in table.GetAll()
join s in refTableStatuses.GetAll() on j.Status equals s.Key2
join t in refTableTypes.GetAll() on j.Type equals t.Key2
select new Job
{
Key1 = j.Key1,
Key2 = j.Key2,
Title = j.Title,
Status = s.Title, // value from Ref (Status) s
Type = t.Title // value from Ref (Type) t
};
You probably need to somehow distinguish between status and type (as they come from same table and are represented by same entity). Since you didn't explain how you differentiate between them, I updated my query variables to reflect that.