I have a DataTable I would like to filter based on elements in a Dictionary.
The Dictionary key is an integer and the value portion is a class.
The contained class has a field called ItemId. This ItemId is a field in the datatable.
I have a value for the key portion of the dictionary.
What I would like is a LINQ query which returns an Enumerable subset of the datatable based on the value I have for the key.
In other words I want a result set of all records in the datatable whose ItemId column is in records contained in the dictionary for which I have the Key value.
Is this possible?
If I understand, you have a key. So with the key, you have one value of the dictionary. This seems very simple to obtain what you want :
int key = 3;
var enumerable = dataTable.Where(t => t.ItemId == dictionary[key].ItemId);
Related
I have a table user which contains an array Employee which stores the names ,i need to retrieve the last element of the array.
Could not figure out.Tried with count but count gives value =1 but there are three elements in an array
r.table("user")("Employee").count()
I need count actual elements in Employee array which is 3.Please help.Thanks
If you don't call .count on a specific document's field, it will return the count of the sequence of documents. In your request, 1 as a result actually means there is one document in the table.
You need to loop over each document to fetch the last name for each document, and you can fetch it directly using .nth:
r.table("user").map(function(document) {
return document.merge({lastName: document("Employee").nth(-1)});
// or, if you like complicated stuff
return {
id: document('id'),
lastName: document("Employee").nth(
document("Employee").count().sub(1)
)
};
});
I am using Asp.net mvc3
In my viewmodel I have taken an IEnumerable attributelike:
public IEnumerable<EmployeeDetailsViewModel> EmployeeList{get;set;}
In my controller this EmployeeList is having a list of 2*2 matrix. i.e. I have passed an array to this list.
My array name is :employee
attributes:
First Name
Last Name
Task
after result it is displaying employee list in EmployeeList like EmployeeList[0]: values of First Name, Last Name,Task
now I want to store the list values in table in database.
My table also contains similar fields like my array attributes
I am using sql Server 2008 as backend
I want to do this in my controller.
how can I do this?
do I need to use foreach loop to assign list values to attributes of table or need to do something else?
please suggest me something...
I myself resolved the problem:
can use foreach loop here like:
foreach (ModelName item in viewModel.EmployeeList)
{
TableName a = new TableName();
a.Project1up = item.FirstName;
a.Employee1up = item.LastName;
a.Allocation = item.Task;
}
How can i compare array of elements with the linq query. I will pass array of elements to the controller and i want to compare and display the records that only contains the passed elements.
I will pass an array of values like ["first","second","third"] and i want to compare these records with the linq query and generate the result that contains these three records
I'm not sure but you probably want something like this
String[] values = {"first", "second", "third"};
List<YourObject> query =
(from p in this.db.table
where values.Contains(p.values) select p).ToList();
I already have a variable containing some groups. I generated that using the following LINQ query:
var historyGroups = from payee in list
group payee by payee.Payee.Name into groups
orderby groups.Key
select new {PayeeName = groups.Key, List = groups };
Now my historyGroups variable can contain many groups. Each of those groups has a key which is a string and Results View is sorted according to that. Now inside each of those groups there is a List corresponding to the key. Inside that List there are elements and each one those element is an object of a particular type. One of it's fields is of type System.DateTime. I want to sort this internal List by date.
Can anyone help with this? May be modify the above query or a new query on variable historyGroups.
Thanks
It is not clear to me what you want to sort on (the payee type definition is missing as well)
var historyGroups = from payee in list
group payee by payee.Payee.Name into groups
orderby groups.Key
select new {
PayeeName = groups.Key,
List = groups.OrderBy(payee2 => payee2.SomeDateTimeField)
};
Is most straightforward.
If you really want to sort only by date (and not time), use SomeDateTimeField.Date.
Inside that List there are elements and each one those element is an object of a particular type. One of it's fields is of type System.DateTime
This leads me to maybe(?) suspect
List = groups.OrderBy(payee2 => payee2.ParticularTypedElement.DateTimeField)
Or perhaps even
List = groups.OrderBy(payee2 => payee2.ObjectsOfParticularType
.OfType<DateTime>()
.FirstOrDefault()
)
I hope next time you can clarfy the question a bit better, so we don't have to guess that much (and come up with a confusing answer)
I have a ComboBox being bind to LINQ object as below.
Dim LearnTypeList = context.LearnTypes.OrderBy(Function(a) a.LearnType).ToList()
dlLearnedAbout.DataSource = LearnTypeList
dlLearnedAbout.DisplayMember = "LearnType"
dlLearnedAbout.ValueMember = "LearnType"
I am not able to use Index of Items to find Item with matching text as below .
MessageBox.Show(dlLearnedAbout.Items.IndexOf("Website"))
This always returns -1 even if its there inside the table and dropdown.Is it because the Item bound to the Dropdown is of type "LearnTypes ?
IndexOf("Website") is looking through the list for an item of type String. your list doesn't contain strings though - it contains objects of whatever type you have in context.LearnTypes. that's why you are getting -1 (aka item not found)