Obtain all records received within last six months - c#

New to Linq to Entity, trying to get all records received within the last six months. I have spent the last several hours trying to get this to work. Any assistance would be greatly appreciated. When I call the 'limit' variable it is being assigned the date 01/01/0001. Any assistance would be appreciated. It works if I comment out the 'where' clause; however, I need it to be sorted by only the last six months.
Thanks in advance.
JobSeekersEntities context = new JobSeekersEntities();
var limit = DateTime.Today.AddMonths(-6);
var query = from c in context.Applications
where c.received > limit
orderby c.received descending
select new { c.firstName, c.middleName, c.lastName, c.street, c.city, c.state, c.zip, c.position };
var results = query.Take(25).ToList();
applicationDataGrid.DataContext = results;

If you stop the debugger at the line "var limit = " you will get that value. You need to press F10 to step over that code then look at value, it will be correct. Have to let that line run so that limit gets assigned. Var in this case is DateTime, which is a value type so it has a default value. I could see this being misleading.

Related

How to optimize C# mongodb query on large datadases?

I have a database table having 100 million records. Screen Shot is taken from Robomongo
Table Schema: There are 100 million records
When I run the following code. I get results, but It takes around 1 minute to get completed. I need to optimize the query to get results faster. What I have done till now is here. Please tell me the way forward to achieve the optimized result.
var collection = _database.GetCollection<BsonDocument>("FloatTable1");
var sw = Stopwatch.StartNew();
var builder = Builders<BsonDocument>.Filter;
int min = Convert.ToInt32(textBox13.Text); //3
int max = Convert.ToInt32(textBox14.Text); //150
var filt = builder.Gt("Value", min) & builder.Lt("Value", max);
var list = collection.Find(filt);
sw.Stop();
TimeSpan time = sw.Elapsed;
Console.WriteLine("Time to Fetch Record: " + time.ToString());
var sw1 = Stopwatch.StartNew();
var list1 = list.ToList();
sw1.Stop();
TimeSpan time1 = sw1.Elapsed;
Console.WriteLine("Time to Convert var to List: " + time1.ToString());
Console.WriteLine("Total Count in List: " + list1.Count.ToString());
Out Put is:
Time to Fetch Record: 00:00:00.0059207
Time to Convert var to List: 00:01:00.7209163
Total Count in List: 1003154
I have few question related to the given code.
When line collection.Find(filt) executes, does it fetch filtered record from the database OR Just creating filter?
var list1 = list.ToList(); takes 1 minute to execute, is it only converting from var to list OR First fetching data than converting?
How to achieve this query and result in least possible time. Please Help.
When line collection.Find(filt) executes, does it fetch filtered
record from the database OR Just creating filter?
It is just creating the filter.
var list1 = list.ToList(); takes 1 minute to execute, is it only
converting from var to list OR First fetching data than converting?
It is fetching the data and converting.
How to achieve this query and result in least possible time. Please Help.
The fetch / filtering on the database is eating your time. The easiest way to speed it up would be creating an index on the column you are filtering.
Everything else would need some more effort or database technologies, like creating a column which more roughly presents your date (e.g. grouped by day) and indexing this one, or creating something like table sections grouped by a given timespan (I'm not a DB-Admin and don't know the proper terms for this, but I remember somebody doing it on a database with billions of records ;) )

How to perform addition in a Linq query

My query is like
var query = dbContext.table1.join(dbcontext.table2,i=>i.table1.id,j=>j.table2.id,
(i,j)=>new {
name = i.name,
hours = (new decimal?[]{ j.day1,j.day2,j.day3}.Sum()),
total = ???????
}).ToArray();
In the hours field I am getting the values of individual user's working hours for three days. In the "total" field I want to display the sum of all users' "hours" values.
Can you tell me how to get the "total" value?
var total = query.Sum(x => x.hours);
Since this total is for all rows in the result set, you do not want one value for each row, but one value representing the aggregate of the entire array.

LinqToExcel: How do I exclude certain rows?

I've been struggling with this for a few days now and I'm stumped. I'm hoping that someone can provide an alternate suggestion.
Basically, I'm reading data from excel using LinqToExcel. But I want to exclude all rows with a "Rating" of "NR". Here's a sample of my data:
CompanyName Rating SalesMan
Apple 2 Steve
Google NR Steve
Microsoft 3 John
Dell 1 Steve
Pepsi 3 John
I just want to find all companies that belong to Steve but doesn't have a rating of "NR". My final list should be:
CompanyName SalesMan
Apple Steve
Dell Steve
I've tried the following code but it doesn't work:
1)
var masterList = masterDataXL.Worksheet("data_all").Where(d => !d["Rating"].Equals("NR"));
2)
var masterList = masterDataXL.Worksheet("data_all")
.Where(m =>
!m["Rating"].Equals("NR")
&&
m["SalesMan"].ToString().Contains(resAnLastName)) // check for last name
.Select(m => new ResAnTicksDataClass
{
Company = m["CompanyName"],
Rating = m["Rating"],
Seller = m["SalesMan"]
}).AsEnumerable();
3) Created a property for Rating and did the following:
var masterList = masterDataXL.Worksheet("data_all")
.Where(m =>
m["Analyst"].ToString().Contains(resAnLastName)) // check for last name
.Select(m => new ResAnTicksDataClass
{
Company = m["CompanyName"],
Rating = m["Rating"],
Seller = m["SalesMan"]
}).AsEnumerable();
var dataList = (from m in masterList
where m.Rating != "NR"
select new ResAnTicksDataClass
{
ResAnName = m.ResAnName,
DescrTick = m.DescrTick
}).AsEnumerable();
I'm open to any other suggestions that you might have because I'm completely stumped. Thank you so much in advance.
I suggest you select the 'Rating' column in your Excel file and do a search & replace on the selection (CHange 'NR' to '0') and then filter. Should help using a single data type.
As phoog said, converting Excel files into a table, that table will need to specify each column's type. To do so, it'll look only the 10 first rows of your Excel file. So if your file doesn't have a 'NR' value in the first 10 rows, it will set the column type to INT, and therefore fail to convert the value 'NR'.
A simple trick to fix this is to add a row to your Excel file, just before your first data row, with the data using the datatype you want to use.
As an example, if a column is using text values and sometimes the text is using over 255 caracters, make sure the first 10 rows have at least 1 text value using 256 caracters. Else, once it creates the table, the column will be set to VARCHAR(255) instead of VARCHAR(MAX) and then crash while converting texts longer than 255 caracters.
Conclusion: always make sure the first 10 rows are using the right type and size to fit all the rows of your Excel file!
In you first sample you should change this:
d => !d["Rating"].Equals("NR")
to this:
d => d["Rating"] != "NR"
It could also be written in a cleaner way:
var masterList =
from d in masterDataXL.Worksheet("data_all")
where d["Rating"] != "NR"
select d;

Index out of range exception when using this query from C#

I am using a calculation in my SQL query. How can I use that calculated field in C#? When I try, I get an index out of range exception.
My query is:
Select OwnerCompanyLog.olog_name,inlt_companyid,inlt_childcompid,inlt_effectinterest,inlt_percent,inlt_sharetype,inlt_shares,inlt_childbase,inlt_effdate,
((inlt_percent * inlt_effectinterest)/100)eff
from InterestLogTable
INNER JOIN OwnerCompanyLog
ON
InterestLogTable.inlt_childcompid = OwnerCompanyLog.olog_companyid
where inlt_companyid=5
Order By inlt_childcompid
I want to use inlt_percent * inlt_effectinterest)/100 in my C# code:
entity.ParentCompany = new List<Company>();
while (parentCompanyReader.Read())
{
ParentCompany.Effect = parentCompanyReader["eff"].ToString();
entity.ParentCompany.Add(ParentCompany);
}
parentCompanyReader.Close();
But I got the error above.
the problem is only with calculated field. The above comment is absolutely correct. It could be your parentCompanyReader class that has the error. Meanwhile could you try few things to confirm the error.
Move the eff column to some middle. It could be that your parentCompanyReader would not be able to deal with only a limited number of column.
Also what is "eff" here, a column ? Also put a AS after the calculated column.
Select (inlt_percent * inlt_effectinterest)/100)eff AS EFF, OwnerCompanyLog.olog_name,inlt_companyid,inlt_childcompid,inlt_effectinterest,inlt_percent,inlt_sharetype,inlt_shares,inlt_childbase,inlt_effdate
Note : these are just arrows in dark !
Try to use it with index rather than column name .
while (parentCompanyReader.Read())
{
ParentCompany.Effect = Convert.ToInt32(parentCompanyReader[5]);
entity.ParentCompany.Add(ParentCompany);
}
parentCompanyReader.Close();
Im assuming that the calculated field returns an integer
and also make sure u use using statement so that incase if there is an error then connection will be closed and disposed

Add minutes from one column to datetime column in linq query

I have a column in my database named Created which is a datetime and a column named Minutes which is a varchar(5) and will contains values such as 0, 60, 120, etc.
How can I write a LINQ statment that only returns those records:
where Created + Minutes has gone passed the current time?
Thank you!
--- Update: code so far ---
var results = from emails in MyDAL.Context.Emails
emails.Created.Value.AddMinutes(double.Parse(emails.Minutes)) > DateTime.Now
select emails;
For others looking for the answer, here is how I ended up making it work....
System.Data.Objects.EntityFunctions.AddMinutes(emails.Created, emails.Minutes) < DateTime.Now
Well you could try:
DateTime now = DateTime.UtcNow;
var query = from record in db.Records
where record.Create.AddMinutes(int.Parse(record.Minutes)) > now
select record;
Whether that will work or not depends on the LINQ provider...

Categories