How to get only specific field from the list - c#

I have an IEnumerable of Lesson objects:
IEnumerable<Lesson> filteredLessons
I convert it to a List through the following method:
ToList();
But I want the returned list to contain only the first property, lessonid, not all the Lesson properties.
How can I get the data of specific property of the list instead of the objects?

You can select the value you want first, like this:
filteredLessons.Select(l => l.lessonId).ToList();
And you'll get a list of ID's

If you want to get the the specific row value from list using linq use the following code:
var name = from r in objClientList
where r.ClientCode == Convert.ToInt32(drpClientsInternal.Items[i].Value)
select r.IsInternalClient;
foreach (bool c in name)
{
if (c)
{
ClientNameInternal = ClientNameInternal + drpClientsInternal.Items[i].Text +", ";
drpClientsInternal.Items[i].Selected = true;
}
}

Related

Finding a specfic int in a List

I have a list which is created from a Class:
class Vara
{
public int streckKod { get; set; }
public string artNamn { get; set; }
}
And the list looks like this:
List<Vara> minaVaror = new List<Vara>();
And I add to the list with this line:
minaVaror.Add(new Vara() {streckKod = inputBox1, artNamn = textBox2.Text });
But what I'm stuck at is how I can find a specific int within the list. Let's say I'm searching for the item in my list holding the number 293 in the variable streckKod.
I've tried using .IndexOf function but I haven't gotten it to work properly.
Also would it be possible to get the item number that the specific number is located in?
If you want to find all items whose streckKod value is 293 use Where
var items = minaVaror.Where(i => i.streckKod == 293);
If interested in only first item use FirstOrDefault -
var item = minaVaror.FirstOrDefault(i => i.streckKod == 293);
FirstOrDefault will return null in case no item exist in collection with value 293.
Make sure you add namespace System.Linq in your class to use these LINQ extension methods.
Use Linq
minaVarror.Find(item=>item.strekKod == 293);
Adding to Rohit Vats answer...
When you have found your item, with either Where or FirstOrDefault you can get the index by doing:
var item = minaVaror.FirstOrDefault(i => i.streckKod == 293);
// Get index of Vara (item) with streckKod = 293
int index = minaVaror.IndexOf(item);
As IndexOf returns the position of an exact item (Vara) within the list minaVaror

Arrays/Array Lists

I am fairly new to C#
I am trying to retrieve some information from an external data source and store it in array, once it is in an array I wish to sort it by time.
I know how to do this for just one column in a row, however the information I require has multiple columns.
For example:
foreach (Appointment Appoint in fapts)
{
// Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
}
// Sort my array by Appoint.Start
foreach ( item in myNewArray )
{
//print out Appoint.Subject - Appoint.Start, Appoint.Organiser.Name.ToString() and Appoint.location
}
Many thanks for your help.
EDIT:
I have multiple data sources which pull in this:
foreach (Appointment Appoint in fapts)
{
// Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
}
Hence the need to sort the items in a new array, I know this isn't very efficent but there is no way of getting the information I need in any other way.
You can sort a list using the LINQ sorting operators OrderBy and ThenBy, as shown below.
using System.Linq;
and then...
var appointments = new List<Appointment>();
var sortedAppointments = list.OrderBy(l => l.Subject).ThenBy(l => l.Name).ToList();
This will create a new list of appointments, sorted by subject and then by name.
It's unclear what your final aim is but:
Use a generic List instead of an array:
See this SO question for more information as to why using a List is prefered.
List<Appointment> appointments = new List<Appointment>();
foreach (Appointment Appoint in fapts)
{
appointments.Add(Appoint);
}
foreach (var item in appointments)
{
Console.WriteLine(item.Subject);
Console.WriteLine(item.Foo);
// Here you could override ToString() on Appointment to print eveything in one Console.WriteLine
}
If the aim of your code is to order by time, try the following:
var sortedAppointments = fapts.OrderBy(a => a.Start); // assuming Start is a DateTime property of `Appointment`.
Consider a Dictionary Object instead of an array if the data is conceptually one row multiple columns.
foreach(KeyValuePair<string, string> entry in MyDic)
{
// do something with entry.Value or entry.Key
}
You already have a list of objects in fpts, sort that list itself:
fpts.OrderBy(x => x.Subject).ThenBy(x => x.Location).ToList();
LINQ is your friend here.
fapts appears to already be a collection so you could just operate on it.
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start).ToArray()
I've used the ToArray() call to force immediate evaluation and means that myNewArray is already sorted so that if you use it more than once you don't have to re-evaluate the sort.
Alternatively if you are only using this once you can just as easily miss the ToArray() portion out and then execution of the sort will be deferred until you try and enumerate through myNewArray.
This solution puts the source objects into the array, but if you are just wanting to store the specific fields you mention then you will need to use a select. You have two choices for the array item type, you can either use an anonymous class which provides difficulties if you are returning this array from a function or define a class.
For anonymous:
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
.Select(Appoint => new {
Start = Appoint.Start,
Organiser = Appoint.Organiser.Name.ToString(),
Location = Appoint.Location
}).ToArray();
For named class assuming class is MyClass:
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
.Select(Appoint => new MyClass {
Start = Appoint.Start,
Organiser = Appoint.Organiser.Name.ToString(),
Location = Appoint.Location
}).ToArray();
You have a wide range of options. The 2 most common are:
1) Create a class, then define an array or list of that class, and populate that
2) Create a structure that matches the data format and create an array or list of that
Of course, you could put the data into an XML format or dataset, but that's probably more work than you need.
public List<foo> appointments = new List<foo>();
public struct foo
{
public string subject ;
public DateTime start ;
public string name ;
public string location ;
}
public void foo1()
{
// parse the file
while (!File.eof())
{
// Read the next line...
var myRecord = new foo() ;
myRecord.subject = data.subject ;
myRecord.start = data.Start ;
myRecord.name = data.Name ;
//...
appointments.Add(myRecord);
}
}
Enjoy
(Since I can't comment and reply to the comment - it wasn't clear if he had a class, etc. or was just showing us what he wanted to do. I assumed it was just for demonstration purposes since there wasn't any info as to how the data was being read. If he could already put it into a class, than the first answer applied anyway. I just tossed the last 2 in there because they were options for getting the data first.)

Access my class returned list collection in code behind

I have a list collection in my class library that uses a sql datareader to returns a list of family details
public class Dataops
{
public List<Details> getFamilyMembers(int id)
{
some of the database code..
List<Details> fammemdetails = new List<Details>();
Details fammember;
while (reader.Read())
{
fammemdetails = new Details((
reader.GetString(reader.GetOrdinal("PHOTO")));
fammemdetails.add(fammember);
}
return fammemdetails;
}
}
So i reference the dll to my project and would like to bind an image to one of my datareader values.
MyProject
DataOps ops = new DataOps();
myimage.ImageUrl = ??? (how do i access the list collections return image value here?
I am able to bind a datasource to the entire method like so
dropdownlistFamily.DataSource = mdb.GetFamilyMembers(id);
But cant figure out how to just grab a single value from there
You can use First/FirstOrDefault, Single/SingleOrDefault depending on your requirement. This would give you a single item from the List and you can access its ImageUrl property.
var item = mdb.GetFamilyMembers(id).FirstOrDefault();
if(item != null)
myimage.ImageUrl = item.ImageUrlProperty;
If you want to get some specific object from the list based on the condition then you can do:
var item = mdb.GetFamilyMembers(id).FirstOrDefault(r=> r.ID == someID);
You may see: LINQ Single vs SingleOrDefault vs First vs FirstOrDefault
You can use FirstOrDefault or SingleOrDefault. Or specify a predicate and use Where.
var firstValue = ops.getFamilyMembers(1).FirstOrDefault();
Use index to access particular record in the collection. You will need to ensure that element exists at the index you given in indexer, otherwise you will get exception. It is zero based index so first element will be at zero index.
var familyMembers = mdb.GetFamilyMembers(id);
if(familyMembers.Count > 0)
myimage.ImageUrl = familyMembers[0].ImageURLProperty;

gridview with arraylist not showing data

I have a gridview that I populate with values I get from a powershell command. For example my powershell command is get-command. I know the command returns the values. Here is my code however my gridview never shows the data.
ArrayList boxesarray = new ArrayList();
foreach (PSObject ps in commandResults)
boxesarray.Add(ps.Properties["Name"].Value.ToString());
boxes.DataSource = boxesarray;
boxes.DataBind();
I know the value is there because I replaced the last two lines with a label and was able to see the value.
boxlabel.text = boxesarray[4];
I must be missing something. Help please.
The GridView requires a collection or IEnumerable of classes which have properties, and the properties are mapped to columns.
An array like yours have value typed objects (strings) which has no roperties, so you can't bind the properties to the columns.
ArrayList boxesarray = new ArrayList();
You could create a simple class like this:
public class PropertyContainer
{
public string Value {get;set;}
}
// NOTE: you can override ToString(); to customize String.Format behaviour
// and to show it in the debugger (althought there's other way for this, using
// DebuggerDisplayAttribute)
And create and populate an array of this class, which will be correctly bound to the datagrid.
foreach (PSObject ps in commandResults)
boxesarray.Add(
new PropertyContainer { Value = ps.Properties["Name"].Value.ToString()});
boxes.DataSource = boxesarray;
boxes.DataBind();
Other option is to convert your array to an array of objects using LINQ. You can even use anonymous object if the data grid columns are set to be automatically created.
// anonymous type
var dataForBinding = boxesArray.select(val => new {Value = val});
// array of the class
var dataForBinding = boxesArray.select(val => new PropertyContainer
{ Value = val });
You can bind this data to your gridview, and it will work perfectly.
You can try
.DataSource = (from ps in commandResults
select { Name:ps.Properties["Name"].Value.ToString() }).ToList();
Or
.DataSource = (from name in yourarraylist
select { Name:name.ToString() }).ToList();

LINQ - Select * from XML elements with a certain tag

I've been looking at an example of LINQ from the following link; I've posted the code below the link.
Is it possible to modify this example so that the items returned in var contain all sub elements found in the items matching the doc.Descendants("person") filter? I basically want this XML query to act like a SQL select * so I don't have to explicitly specify field names like they've done with drink, moneySpent, and zipCode.
http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html#example_1
static void QueryTheData(XDocument doc)
{
// Do a simple query and print the results to the console
var data = from item in doc.Descendants("person")
select new
{
drink = item.Element("favoriteDrink").Value,
moneySpent = item.Element("moneySpent").Value,
zipCode = item.Element("personalInfo").Element("zip").Value
};
foreach (var p in data)
Console.WriteLine(p.ToString());
}
The OP said he liked the answer posted, so I'll just resubmit it for science :)
var data = from item in doc.Descendants("person")
select item;
The only problem with this is that data is an IEnumerable<XElement>, and you'll have to query the fields by string names.
// Do a simple query and print the results to the console
var data = from item in doc.Descendants("person")
select item;

Categories