LINQ to SQL Get Latest Date from Object - c#

I have read a few items on this, including How to select only the records with the highest date in LINQ but I don't know how to apply it to my case which is slightly more complex.
I am trying to get all AdjusterProfileStatusItem but only select the most recent s.statusDate. Currently, the query just returns all dates for all records; whereas I just want the most recent date for all records.
(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"
&& s.statusDate.Max() // causes syntax error...
select new AdjusterProfileStatusItem
{
user = u,
adjuster = a
})
Edit:
I have also tried this which gives me a syntax error...
(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"
group new { u, a, s } by s.adjusterID into x
select new AdjusterProfileStatusItem
{
user = u, // u does not exist in context
adjuster = a, // a does not exist in context
status = x.Max(y => y.statusDate) // anonymous type does not contain definition for 'statusDate'
})

I'm not sure how you feel about Lambda expressions but I would probably do this:
db.Users
.Join(db.Adjusters,
u => u.Id,
a => a.UserId,
(u, a) => new
{
User = u,
Adjuster = a
})
.Join(db.AdminAdjusterStatus,
a => a.Adjuster.Id,
s => s.AdjusterId,
(a, s) => new
{
User = a.User,
Adjuster = a.Adjuster,
AdminAdjusterStatus = s
})
.Where(x => x.User.userType == "adjuster"
&& x.AdminAdjusterStatus.status == "approved"
&& x.AdminAdjusterStatus.statusDate == db.AdminAdjusterStatus
.Where(y => y.AdjusterId ==
x.AdminAdjusterStatus.AdjusterId)
.Max(z => z.statusDate))
.Select(a => new AdjusterProfileStatusItem
{
user = a.User
adjuster = a.Adjuster
})
**EDIT!!!**
(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"
&& s.statusDate == GetMaxStatusDate(db.AdminAdjusterStatus.ToList(), s.AdjusterID)
select new AdjusterProfileStatusItem
{
user = u,
adjuster = a
})
private DateTime GetMaxStatusDate(List<AdminAdjusterStatus> statuses, int adjusterId)
{
return (from a in statuses
where a.AdjusterId == adjusterId
group a by a.AdjusterId into values
select values.Max(x => x.statusDate)).FirstOrDefault();
}
OR
(from u in db.Users
join a in db.Adjusters
on u.id equals a.userID
join s in db.AdminAdjusterStatus
on a.id equals s.adjusterID
where u.userType.ToLower() == "adjuster"
&& s.status.ToLower() == "approved"
&& s.statusDate == db.AdminAdjusterStatus
.Where(x => x.AdjusterId == s.AdjusterId)
.Select(y => y.statusDate)
.Max();
select new AdjusterProfileStatusItem
{
user = u,
adjuster = a
})

Related

How to format Datetime column inside Contains method

I have database table called Absence, which has a column called ABSDate. The type of this column is Datetime.
I'm trying to use a LINQ query to search inside this table. When I use the Contains method to query against other columns, I get results:
Working Query
dataGridView1.DataSource = (from A in context.Absence
join S in context.Stagiaire on A.STG equals S.ID
join G in context.Groupe on S.GRP equals G.CODE
join F in context.FILERE on G.FL equals F.CODE
select new { A.ID, A.ABSDate, A.STG, S.Nom, S.Prenom, S.GRP, G.FL })
.Where(X => X.STG.Contains(SearchBox.Text) ||
X.Nom.Contains(SearchBox.Text) ||
X.Prenom.Contains(SearchBox.Text) ||
X.GRP.Contains(SearchBox.Text))
.Select(x => new { x.ID, Date = x.ABSDate, x.Nom, Prénom = x.Prenom, Filiére = x.FL, Groupe = x.GRP })
.ToList();
However, when I use ABSDate as part of the query, I get the following error:
System.NotSupportedException: 'LINQ to Entities does not recognize the
method 'System.String ToString(System.String)' method, and this method
cannot be translated into a store expression.'
Broken Query
dataGridView1.DataSource = (from A in context.Absence
join S in context.Stagiaire on A.STG equals S.ID
join G in context.Groupe on S.GRP equals G.CODE
join F in context.FILERE on G.FL equals F.CODE
select new { A.ID, A.ABSDate, A.STG, S.Nom, S.Prenom, S.GRP, G.FL })
.Where(X => X.STG.Contains(SearchBox.Text) ||
X.Nom.Contains(SearchBox.Text) ||
X.Prenom.Contains(SearchBox.Text) ||
X.GRP.Contains(SearchBox.Text) ||
X.ABSDate.ToString("dd/MM/yyyy hh:mm").Contains(SearchBox.Text))
.Select(x => new { x.ID, Date = x.ABSDate, x.Nom, Prénom = x.Prenom, Filiére = x.FL, Groupe = x.GRP })
.ToList();
Clearly, I know that this is where the issue lies:
X.ABSDate.ToString("dd/MM/yyyy hh:mm").Contains(SearchBox.Text)
How can I format the SearchBox value to dd/MM/yyyy hh:mm, and search by it using Contains method?
There are two ways to go about this:
1st - client side evaluation
dataGridView1.DataSource = (from A in context.Absence
join S in context.Stagiaire on A.STG equals S.ID
join G in context.Groupe on S.GRP equals G.CODE
join F in context.FILERE on G.FL equals F.CODE
select new { A.ID, A.ABSDate, A.STG, S.Nom, S.Prenom, S.GRP, G.FL })
.Where(X => X.STG.Contains(SearchBox.Text) ||
X.Nom.Contains(SearchBox.Text) ||
X.Prenom.Contains(SearchBox.Text) ||
X.GRP.Contains(SearchBox.Text)
).AsEnumerable().Where(X => X.ABSDate.ToString("dd/MM/yyyy hh:mm").Contains(SearchBox.Text))
.Select(x => new { x.ID, Date = x.ABSDate, x.Nom, Prénom = x.Prenom, Filiére = x.FL, Groupe = x.GRP })
.ToList();
The second would be to format the textbox SearchBox.Text to the format of datetime.
Bear in mind that it should be formatted as a datetime, as there is no like equivalent for dates in linq to sql.
Solution small developed from Athanasios Kataras answer
dataGridView1.DataSource = (from A in context.Absence
join S in context.Stagiaire on A.STG equals S.ID
join G in context.Groupe on S.GRP equals G.CODE
join F in context.FILERE on G.FL equals F.CODE
select new { A.ID, A.ABSDate, A.STG, S.Nom, S.Prenom, S.GRP, G.FL })
.AsEnumerable().Where(X => X.STG.Contains(SearchBox.Text) || X.Nom.Contains(SearchBox.Text) || X.Prenom.Contains(SearchBox.Text) || X.GRP.Contains(SearchBox.Text)|| X.ABSDate.ToString("dd/MM/yyyy hh:mm").Contains(SearchBox.Text))
.Select(x => new { x.ID, Date = x.ABSDate, x.Nom, Prénom = x.Prenom, Filiére = x.FL, Groupe = x.GRP }).ToList();
Thanks again Athanasios Kataras

VB.NET to C# Linq

I have the following LINQ
Dim z = (From d In db.GPSdevice
Where d.CompanyId = currentuser.CompanyId And d.Type = "Truck" Or d.Type = "Trailer"
Order By d.ListOrder Descending
Group d By d.Driver Into g = Group
Select g.FirstOrDefault())
I try to convert it to c#
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => new { Group = g });
but not sure, how to convert Select g.FirstOrDefault()...
You can use the query syntax in C# too, no need to rewrite using the extension methods directly:
var z = (from d In db.GPSdevice
where (d.CompanyId == currentuser.CompanyId) && (d.Type == "Truck") || (d.Type == "Trailer")
orderby d.ListOrder descending
group d by d.Driver into g = group
select g.FirstOrDefault())
Just call g.FirstOrDefault() in your Select
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => g.FirstOrDefault());

Linq AND searches

I have a array called searchWords, that is a dynamic array that stores peoples search words. I need to add an option for AND search. So the search will only retrieve items if both variables in searchWords contains for resultList. Now it is searchWords.Any. Will searchWords.All make this works?
var resultList = from c in context.Category
join q in context.Question on c.CategoryId equals q.CategoryId
join qf in context.QuestionFilter on q.QuestionId equals qf.QuestionId
join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
from a2 in QuestAnsw.DefaultIfEmpty()
orderby c.SortOrder
orderby q.SortOrder
where qf.FilterId == filterKeyAsInt
&& q.Published == true
&& c.Published == true
&& q.CustomerId == customerId
&& (searchWords.Any(w => a2.Text.Contains(w))
|| searchWords.Any(w => c.Text.Contains(w))
|| searchWords.Any(w => q.Text.Contains(w)))
select new { Category = c, Question = q };
You can put multiple clauses inside an All(), e.g.
&& (searchWords.All(w =>
a2.Text.Contains(w) &&
c.Text.Contains(w) &&
q.Text.Contains(w)))
...
You can do this if use searchWords.All, but i think searchWords.Any is more intuitive.
var resultList = from c in context.Category
join q in context.Question on c.CategoryId equals q.CategoryId
join qf in context.QuestionFilter on q.QuestionId equals qf.QuestionId
join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
from a2 in QuestAnsw.DefaultIfEmpty()
orderby c.SortOrder
orderby q.SortOrder
where qf.FilterId == filterKeyAsInt
&& q.Published == true
&& c.Published == true
&& q.CustomerId == customerId
&& !
(
searchWords.All(w => !a2.Text.Contains(w))
&& searchWords.All(w => !c.Text.Contains(w))
&& searchWords.All(w => !q.Text.Contains(w))
)
select new { Category = c, Question = q };

How to Select new model list after GroupBy mothod in Linq?

I want select grouped rows to a new model list.this is my code:
List<Model_Bulk> q = (from a in db.Advertises
join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
where a.AdvertiseActive == true
&& a.AdvertiseExpireDate.HasValue
&& a.AdvertiseExpireDate.Value > DateTime.Now
&& (a.AdvertiseObjectType == 1
|| a.AdvertiseObjectType == 2)
select c)
.GroupBy(a => a.CompanyID).Select(a => new Model_Bulk
{
CompanyEmail = a.CompanyContactInfo.Email,
CompanyID = a.CompanyID,
CompanyName = a.CompanyName,
Mobile = a.CompanyContactInfo.Cell,
UserEmail = a.User1.Email,
categories = a.ComapnyCategories
}).ToList();
After group by, i can not use Select and naturally this syntax error raised:
System.Linq.IGrouping' does not contain a definition for 'CompanyContactInfo' and no extension method 'CompanyContactInfo' accepting a first argument of type
System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)
If i try with SelectMany() method.but the result will repeated and groupby method not work properly:
List<Model_Bulk> q = (from a in db.Advertises
join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
where a.AdvertiseActive == true
&& a.AdvertiseExpireDate.HasValue
&& a.AdvertiseExpireDate.Value > DateTime.Now
&& (a.AdvertiseObjectType == 1
|| a.AdvertiseObjectType == 2)
select c)
.GroupBy(a => a.CompanyID).SelectMany(a => a).Select(a => new Model_Bulk
{
CompanyEmail = a.CompanyContactInfo.Email,
CompanyID = a.CompanyID,
CompanyName = a.CompanyName,
Mobile = a.CompanyContactInfo.Cell,
UserEmail = a.User1.Email,
categories = a.ComapnyCategories
}).ToList();
Instead of .SelectMany(a => a) you can use .Select(g => g.First()).That will give you the first item of each group.
(from a in db.Advertises
join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
where a.AdvertiseActive == true && a.AdvertiseExpireDate.HasValue && a.AdvertiseExpireDate.Value > DateTime.Now && (a.AdvertiseObjectType == 1 || a.AdvertiseObjectType == 2)
select c)
.GroupBy(a => a.CompanyID)
.Select(g => g.First())
.Select(a => new Model_Bulk
{
CompanyEmail = a.CompanyContactInfo.Email,
CompanyID = a.CompanyID,
CompanyName = a.CompanyName,
Mobile = a.CompanyContactInfo.Cell,
UserEmail = a.User1.Email,
categories = a.ComapnyCategories
}).ToList();
Note that this might not be supported, if that is the case add an AsEnumerable call before .Select(g => g.First())
You should understand that after you do GroupBy() in your LinQ expresstion you work with a group so in your example it will be good to write like this:
List<Model_Bulk> q =
(from a in db.Advertises join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
where a.AdvertiseActive == true
&& a.AdvertiseExpireDate.HasValue
&& a.AdvertiseExpireDate.Value > DateTime.Now
&& (a.AdvertiseObjectType == 1 || a.AdvertiseObjectType == 2)
select c)
.GroupBy(a => a.CompanyID)
.Select(a => new Model_Bulk
{
CompanyEmail = a.First().CompanyContactInfo.Email,
CompanyID = a.Key, //Note this line, it's can be happened becouse of GroupBy()
CompanyName = a.First().CompanyName,
Mobile = a.First().CompanyContactInfo.Cell,
UserEmail = a.First().User1.Email,
categories = a.First().ComapnyCategories
}).ToList();
Instead you could try something like this, instead of mixing query expressions and methods... (using FirstOrDefault() in the where / select as necessary)
(from a in db.Advertises
join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
group a by new { a.CompanyId } into resultsSet
where resultsSet.AdvertiseActive == true && resultsSet.AdvertiseExpireDate.HasValue && resultsSet.AdvertiseExpireDate.Value > DateTime.Now && (resultsSet.AdvertiseObjectType == 1 || resultsSet.AdvertiseObjectType == 2)
select new Model_Bulk
{
CompanyEmail = resultsSet.CompanyContactInfo.Email,
CompanyID = resultsSet.CompanyID,
CompanyName = resultsSet.CompanyName,
Mobile = resultsSet.CompanyContactInfo.Cell,
UserEmail = resultsSet.User1.Email,
categories = resultsSet.ComapnyCategories
}).ToList();

Linq Query, messed up where clause in method

I have a query that should return a sum of total hours reported for the current week.
This code below returns the Correct total of hours but not for a specific user in the database.
public int reportedWeekTime(int currentWeek, string username)
{
var totalTime = (from u in context.Users
from r in context.Reports
from w in context.Weeks
from d in context.Days
where u.Id == r.UserId && r.weekNr.Equals(currentWeek) && r.Id == w.ReportId && w.DayId == d.Id
select d.Hour).DefaultIfEmpty(0).Sum();
return totalTime;
}
The first method returns the number 24, wich is correct but as i said, not for a specific user.
I am trying to do this, but it gives me 0 in return. What am i doing wrong?
public int reportedWeekTime(int currentWeek, string username)
{
var totalTime = (from u in context.Users
from r in context.Reports
from w in context.Weeks
from d in context.Days
where u.Id == r.UserId && r.weekNr.Equals(currentWeek) && r.Id == w.ReportId && w.DayId == d.Id && u.Username.Contains(username)
select d.Hour).DefaultIfEmpty(0).Sum();
return totalTime;
}
Update - Troubleshooting approach, create a new anonymous class with the u.Username property, the string username, and the comparison. It will be easier to visualize what is going on
var users = (from u in context.Users
select new
{
UsernameDb = u.Username,
UsernameSearch = username,
Comparison = u.Username.Contains(username),
}).ToList();
Original
I would modify your query slightly:
Use join's instead of from's with where clauses
Remove the DefaultIfEmpty(0)
(1) Is more for readability, but I think (2) is the cause of your problem
var totalTime = (from u in context.Users
join r in context.Reports on u.Id equals r.UserId
join w in context.Weeks on r.Id equals w.ReportId
join d in context.Days on w.DayId equals d.Id
where r.weekNr.Equals(currentWeek) && u.Username.Contains(username)
select d.Hour).Sum();
I would also make sure that the following query returns result. If not, than that would be your problem
var users = from u in context.Users
where u.Username.Contains(username)
select u;

Categories

Resources