I'm trying to figure how to write a LINQ query that return a list of customers who changed their address on a given date
Cusomters
- Name (nvarchar)
- Address (nvarchar)
- CheckInDate (datetime)
I would first get a list of customers who checked in on a certain date then loop through each customer and get that customer's check ins to see if there are changes. This would result in numerous database queries. Is there a more efficient way to do this?
This will detect all customers who changed their addresses by yourDate.
var result = Customers.Where(c=>c.CheckInDate <= yourDate)
.GroupBy(c=>c.Name)
.Where(g=>g.GroupBy(c=>c.Address).Count() > 1)
.SelectMany(x=>x);
Related
Is there a way to build a QueryExpression returning just a particular set of records?
I have the following Criteria Types:
First:
Returns the first n Records (i.e. select top)
Last:
Returns the last n records
Every:
Returns every n'th record
For the type "First" I can use
queryExpression.TopCount = number_of_records
But I have no Idea how I can achieve the other types of criteria. The issue is that there are quite big data volumes and if I need first to get all records and query the result for example with Linq to customize the resultset I will probably have a performance issue.
If I could build the QueryExpression just selecting exactly what I need the whole thing gets more efficient.
Does anybody have an idea on how to achieve this with a QueryExpression?
The system in question is Microsoft Dynamics CRM Online
For the "last N" you can reverse the sort and use TopCount again.
For the "every Nth" you might want to consider paging the Query Expression.
Say you're looking for every 10th record. What I might do would be to set my page size to 10 (query.PageInfo.Count).
To iterate through the pages as quickly as possible I'd make my "main" query return only the GUIDs. When I retrieve a new page of GUIDs, I'd grab the first GUID and get the columns I want for that record using a separate Retrieve call.
Last N Records: quite simple order by particular field as descinding and then top N that's it
Returns the last n records
// Instantiate QueryExpression QEaccount
var QEaccount = new QueryExpression("account");
QEaccount.TopCount = 5;
// Add columns to QEaccount.ColumnSet
QEaccount.ColumnSet.AddColumns("name", "ah_account_type", "accountid");
QEaccount.AddOrder("name", OrderType.Descending);
Every nth Record:
Do you have any particular criteria here, for example give me all accounts where country =Germany
if yes then you can user condition to return particular set of records as below
// Define Condition Values
var QEaccount_address1_country = "Germany";
// Instantiate QueryExpression QEaccount
var QEaccount = new QueryExpression("account");
// Add columns to QEaccount.ColumnSet
QEaccount.ColumnSet.AddColumns("name", "ah_account_type", "accountid", "address1_country");
// Define filter QEaccount.Criteria
QEaccount.Criteria.AddCondition("address1_country", ConditionOperator.Equal, QEaccount_address1_country);
I need to find New Hires that have been entered into Success Factors and will be starting in the future via OData.
The following C# based query is what I'm using:
DateTime tomorrow = DateTime.UtcNow.AddDays(1);
var newHires = EmpEmployment
.Expand("userNav")
.Where (e => e.startDate >= tomorrow);
The above query returns staff who have a startDate in the future however the User expansion does not find any matching entries and is null.
The issue appears to be that the User table is only populated with entries for the person when the start date is reached. This is a problem because I want to create them in other systems in advance.
Does anyone know if this is normal or thoughts around how I can obtain details like firstname, lastname of future employees who have been entered in the system?
You are trying to use EmpEmployment entity from Employee Central. The required date could be retrieved from RCM (Recruitment Management).
There is no clear answer to your question because customizing depends on the project; you should ask functional consultant responsible for EC what the way to get New hires startDate.
I have a database field like this:
Timestamp varchar(19) utf8mb4_unicode_ci
containing a timestamp (string) like this
"2013-05-29 00:00:00"
I am using the entity framework and I would like to filter by time - meaning I would like to get all entries having a timestamp > (now-interval).
Code would look something like this
var query = from r in db.route
where
r.timestamp > (now-interval);
select r;
How can I do this?
My first suggestion would be to fix the database so the date values are stored as the correct date type. That would solve many issues as well as increase search performance. However, in the (unlikely) situation that you are unable to do that, and that the format of the values in that column all match exactly as you specified in the question, you could convert your local time stamp variable to a string and compare it directly. As the format you have shown has an alphanumeric ordering that is identical to the date order, it should work:
//It's important that this is done here and not inline with the Linq query
var nowInterval = timeStamp.ToString("yyyy-MM-dd HH:mm:ss");
var query = from r in db.route
where string.Compare(r.timestamp, nowInterval, StringComparison.Ordinal) > 0
select r;
I have a table that represents a matrix:
CustType DiscountGroup1 DiscountGroup2 DiscountGroup3
Wholesale 32 10 15
Retail 10 15 0
All my stock items have a corresponding discount group code 1, 2 or 3.
At the time of invoicing I want to lookup the discount the customer type gets on the item(s) being invoiced.
The table needs to be able to grow to include new customer types and new discount groups so nothing can be hardcoded.
I figured I would pull the data into an array so I could select the column by index but I am getting stumped by my entities being too intelligent...
var disc = (from d in context.CustDiscountGroups
where d.CustType == Wholesale
select d).ToArray();
I can only access the columns by name ie: disc[0].DiscountGroup1
if I try disc[0,1] I get an error saying wrong number of indices inside.
What am I missing? I feel like it is something ridiculously fundamental. My only other thought was naming the columns as 1, 2, 3 etc and building a sql select string where I can use a variable to denote a column name.
The database is in design stages as well so the table(s) can be remade in any way needed, I'm struggling to get my head wrapped round which way to approach the problem.
your entity CustDiscountGroups having properties CustType, DiscountGroup1, DiscountGroup2, DiscountGroup3 and your query return array of CustDiscountGroups so you cant access like [0,1] there is no 2D array
if you need to access first item you can get it as disc[0] then you can get any of properties of discount group by name of the property. like
disc[0].CustType, disc[0].DiscountGroup1, disc[0].DiscountGroup2, disc[0].DiscountGroup3
If you want to get array of array then get the property value using reflection as below
var disc = context.CustDiscountGroups.Where(c=>c.CustType == Wholesale)
.Select(v=>typeof(CustDiscountGroups)
.GetFields(System.Reflection.BindingFlags.Public)
.Select(f=>f.GetValue(v)).ToArray())
.ToArray();
var disc = context.CustDiscountGroups.Where(c=>c.CustType == Wholesale)
.Select(v=>typeof(CustDiscountGroups)
.GetProperties()
.Select(f=>f.GetValue(v,null)).ToArray()).ToArray();
now you can access values like disc[0][1]
Please note: I haven't compiled and tested above code, please get the idea and change as you want
i am new to sharepoint.
i have two Sharepoint Lists CustomerList and AnnouncementList
CustomerList with columns(custid,name,region)
AnnoucementList with columns(Annid,AnnouncementText,region)
Now i want to display the Announcements to customers having
CustomerList.region=AnnouncementList.region
Problem is the region is Choice field having multiple values
Eg.
Custid name region
1 Shekhar mumbai,pune
Annid AnnouncementText region
1 Today is holiday pune,gujrat
Now, how to compare each Choice region from CustomerList
with each Choice of AnnouncementList? using CAMEL QUERY
Help appreciated!Even if one match found it must show the data.
thanks!
To compare multi choice value in sharepoint list use SPMultiChoice Class for getting your result instead CAML query.