I have a query like below. anyone have idea why the ?: parts are always return false values although there is item contains in LIST. Or anyone have better idea to write this query, feel free to reply here. thanks.
List EList = new List();
EList.Add(new EmployeeInfo(1, "a1", "b1"));
EList.Add(new EmployeeInfo(2, "a2", "b2"));
List OList = new List();
OList.Add(new EmployeeInfo(1, "a1", "b1"));
OList.Add(new EmployeeInfo(2, "a2", "b2"));
OList.Add(new EmployeeInfo(3, "aa2", "bb2"));
var results = (
from e in b
select new
{
Id = e.ID,
Name = e.Name,
Email = e.Email,
IS_Elist = (EList.Contains(e))?true:false,
IS_Olist = (OList.Contains(e)) ? true : false,
}
).ToList();
First you can simplify your query - expression ? : true : false is equivalent to just expression and we get the following.
var results = b.Select(e => new
{
Id = e.ID,
Name = e.Name,
Email = e.Email,
IS_Elist = EList.Contains(e),
IS_Olist = OList.Contains(e)
})
.ToList();
This should and will definitely yield true if an item from b is contained in EList or OList. If you always get false this means that EList and OList do not contain any items from b. Remember that you are probably doing a comparison of references and therefore
new EmployeeInfo(1, "a", "b") == new EmployeeInfo(1, "a", "b")
will usually yield false.
I guess you actually intended a comparison by ID.
var results = b.Select(e => new
{
Id = e.ID,
Name = e.Name,
Email = e.Email,
IS_Elist = EList.Any(x => x.ID == e.ID),
IS_Olist = OList.Any(x => x.ID == e.ID)
})
.ToList();
The alternative solution is to implement IEquatable<T> on EmployeeInfo or overriding Equals(), GetHashCode() and the equality operator. You should take care that all your implementations yield consistent results or you will end with a lot of confusion why for example Object.Equals() and the equality operator yield different results.
Your equality check may not be behaving as you expect for EmployeeInfo. To confirm, at the bottom of the snippet you posted above, try this and see if it returns true.
var is_olist = OList.Contains(new EmployeeInfo(1, "a1", "b1"));
If this returns false, you need to consider using something simpler but still unique to compare instances (such as the ID member), or else implement equals yourself in EmployeeInfo.
Implement Equals and the Equality == operator in your EmployeeInfo class.
Implementing the Equals Method
Guidelines for Implementing Equals and the Equality Operator (==)
Related
I need to add a join using Lambda if I have a further parameter available that will also be used in a where clause.
My problem is I'm not sure of the exact format for adding a new object MemberTagLysts and then how the where clause should be created.
var tagList = from t in dc.Tags
join b in dc.Businesses on t.BusinessId equals b.BusinessId
where t.IsActive == true
where b.IsActive == true
orderby t.AdImage descending
select new TagItem
{
tagName = t.Name.Replace("\"", ""),
tagImage = tagImagePath + t.AdImage.Replace("\"", ""),
tagDescription = t.Description.Replace("\"", "")
};
if (!string.IsNullOrEmpty(lystId))
{
tagList = (IQueryable<TagItem>)tagList.GroupJoin(dc.MemberTagLysts, a => a.tagId, b => b.TagId, (a, b) => new { a, b });
}
I think you want to do something like this:
var tagList = from t in dc.Tags
join b in dc.Businesses on t.BusinessId equals b.BusinessId
where t.IsActive
where b.IsActive
orderby t.AdImage descending
select new TagItem
{
tagName = t.Name.Replace("\"", ""),
tagImage = tagImagePath + t.AdImage.Replace("\"", ""),
tagDescription = t.Description.Replace("\"", "")
};
if (!string.IsNullOrEmpty(lystId))
{
tagList = tagList
.GroupJoin(dc.MemberTagLysts.Where(l => l.lystId == lystId),
a => a.tagId,
b => b.TagId,
(a, b) => new { a, b }));
}
Conditionally expanding the query is good practice. Note that conditions like where t.IsActive == true are redundant, where t.IsActive is enough and arguable better readable with well-chosen property names (as you have).
First Table is the View and Second is the result I want
This below query works fine
List<BTWStudents> students = (from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
select new BTWStudents
{
StudentId = V.StudentId
Amount= V.PaymentMethod == "Cashier Check" ? V.Amount: "0.00"
}).Distinct().ToList();
But I changed it to List to add string formatting(see below)
List<BTWStudents> students = (from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
select new {V}).ToList().Select(x => new BTWStudents
{
StudentId = V.StudentId
Amount= V.PaymentMethod == "Cashier Check" ? String.Format("{0:c}",V.Amount): "0.00"
}).Distinct().ToList();
With this Second Query I get this
Why is distinct not working in the second query?
When working with objects (in your case a wrapped anonymous type because you are using Select new {V} rather than just Select V), Distinct calls the object.Equals when doing the comparison. Internally, this checks the object's hash code. You'll find in this case, the hash code of the two objects is different even though the fields contain the same values. To fix this, you will need to override Equals on the object type or pass a custom IEqualityComparer implementation into the Distinct overload. You should be able to find a number of examples online searching for "Distinct IEqualityComparer".
Try this (moved your distinct to the first query and corrected your bugged if/then/else):
List<BTWStudents> students = (from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
select new {V}).Distinct().ToList().Select(x => new BTWStudents
{
classId = V.Class.HasValue ? V.Class.Value : 0,
studentName = V.StudentName,
paymentAmount = V.PaymentMethod == "Cashier Check" ? String.Format("{0:c}",x.V.AmountOwed): "0.00"
}).ToList();
You can get around using Distinct all together if you Group by StudentID
var studentsGroupedByPayment =
(from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
group V by V.StudentId into groupedV
select new
{
StudentID = groupedV.Key,
Amount = string.Format("{0:C}",
groupedV.First().PaymentMethod == "Cashier Check" ?
groupedV.First().Amount : 0.0)
}
).ToList();
I have an array filled with long type values and for each value in the array I need to implement a query. I used foreach loop as you can see from the code below:
var result;
foreach(long id in PrdIdArr)
{
var mainQuery = (from o in db.OPERATIONs
join u in db.UNITs on o.OP_UNIT_ID equals u.UNIT_ID into smt
from s in smt
join x in db.XIDs on s.UNIT_ID equals x.UNIT_ID
where o.OP_OT_CODE == OtCode
where x.IDTYP_CD == "BSN"
where s.START_PRD_ID == id
where o.OP_UPD_DATE >= _StartDate
where o.OP_UPD_DATE <= _EndDate
select new
{
o.OP_ID,
o.OP_UPD_DATE,
x.EXTERNAL_ID,
o.OP_OS_CODE,
o.OP_START,
o.OP_ST_STATION,
s.START_PRD_ID
}).Take(_RowNumber);
//var result = mainQuery.ToList();
result.add(mainQuery.ToList());
}
data = this.Json(result);
data.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return data;
However, I have a problem in my code; I have to define a main list just before the foreach loop so that I could add results of each query to the that main list. my question is: How can I define this list as you can see at the beginning of my code? Thanks for the help...
How can I define this list as you can see at the beginning of my code?
Make
new {
o.OP_ID,
o.OP_UPD_DATE,
x.EXTERNAL_ID,
o.OP_OS_CODE,
o.OP_START,
o.OP_ST_STATION,
s.START_PRD_ID
}
into a concrete type (say QueryResult, although something a little more specific than that), and then just declare
var result = new List<QueryResult>();
Also, you should consider turning
foreach(long id in PrdIdArr)
and
where s.START_PRD_ID == id
into
where PrdIdArr.Contains(s.Start_PRD_ID)
var result = new List<object>();
foreach(long id in PrdIdArr)
{
....
result.Add(mainQuery.ToList());
}
You could do this:
var result = PrdIdArr.Select(id =>
from o in db.OPERATIONs
join u in db.UNITs on o.OP_UNIT_ID equals u.UNIT_ID into smt
from s in smt
join x in db.XIDs on s.UNIT_ID equals x.UNIT_ID
where o.OP_OT_CODE == OtCode
where x.IDTYP_CD == "BSN"
where s.START_PRD_ID == id
where o.OP_UPD_DATE >= _StartDate
where o.OP_UPD_DATE <= _EndDate
select new
{
o.OP_ID,
o.OP_UPD_DATE,
x.EXTERNAL_ID,
o.OP_OS_CODE,
o.OP_START,
o.OP_ST_STATION,
s.START_PRD_ID
}
.Take(_RowNumber)
.ToList()
).ToList();
I highly recommend performing some Extract Method refactorings, as the code is pretty complex and hard to understand/mange this way.
Just create the anonymous type outside with the same property names and the correct type
var result = Enumerable.Range(0, 0).Select(x => new
{
OP_ID = 1,
OP_UPD_DATE = DateTime.Now,
EXTERNAL_ID = 1,
OP_OS_CODE = 1,
OP_START = DateTIme.Now,
OP_ST_STATION = "",
START_PRD_ID = 1,
}).ToList();
And in your loop call AddRange
result.AddRange(mainQuery.ToList());
1)
a)
var result1 = from artist in artists
from album in artist.Albums
orderby album.Title,artist.Name
select new { Artist_id = artist.id, Album_id = album.id };
Is the above query translated into:
var result = artists.SelectMany(p => p.albums
.Select(p1 => new { Artist = p, Album = p1 }))
.OrderBy(p2 => p2.Album.Title)
.ThenBy(p3 => p3.Artist.Name)
.Select(p4 => new { Artist_id = p4.Artist.id, Album_id = p4.Album.id });
b)
I'm not sure if this question will make much sense - If my assumptions are correct and thus OrderBy is always one of the last operators to get called ( when using query expression ), then how would we express the following code using query expression (in other words, how do we specify in a query expression that we want OrderBy operator to get called sooner and not as one of the last operators ):
var result = artists
.SelectMany(p1 => p1.albums
.OrderBy(p2=>p2.title)
.Select(p3 => new { ID = p3.id, Title = p3.title }));
2) Do in the following query expression the two orderby clauses get translated into OrderBy(... artist.Name).OrderBy( ... album.Title):
var result1 = from artist in artists
from album in artist.Albums
orderby artist.Name
orderby album.Title
select new { ...};
thank you
For question 1: orderby gets called wherever you show it. Your query isn't quite equivalent to what you showed, but it's close. It doesn't help that you formatted it so that it looks like the Select is called on the result of SelectMany, when it's actually on the arguments to SelectMany. Your query is translated to something more like:
var result = artists
.SelectMany(artist => artist.albums, (artist, album) => new {artist, album})
.OrderBy(z => z.album.Title)
.ThenBy(z => z.artist.Name)
.Select(z => new { Artist_id = z.artist.id, Album_id = z.album.id }
Question 1b) Your query is roughly equivalent to:
var result = from p1 in artists
from p3 in (from p2 in p1.albums
orderby p2.title
new { ID = p2.id, Title = p2.title })
select p3;
It's only a rough translation as nothing in query expressions is converted to that overload of SelectMany, as far as I can remember. On the other hand, it could be that this does what you want in a slightly simpler way:
var result = from p1 in artists
from p3 in p1.albums.OrderBy(p2 => p2.title)
select new { ID = p3.id, Title = p3.title };
You'll still get the ordering within the artist. It's a mixture of query expression and "dot notation", but it looks good to me. Odd that you're not using p1 in the final result, mind you...
For question 2, using two orderby clauses you do indeed get two OrderBy calls, which is almost certainly not what you want. You want:
var result1 = from artist in artists
from album in artist.Albums
orderby artist.Name, album.Title
select new { ...};
That gets translated into the appropriate OrderBy(...).ThenBy(...) calls.
I'm trying to get the following SQL query to work in LINQ:
Select id from table1 where id in (1,2) or canceledId in (1,2)
I'm using BuildContainsExpression to achieve the "IN" condition, but I can't figure out how to implement the "or" condition.
My shot in the dark is as follows:
var identifiers = new List<int> {1,2};
var query = (from t in Context.Table1
select t);
var query =
query.Where(BuildContainsExpression<Table1, int>(t => t.Id, identifiers));
if (showCanceled)
{
var expression = query.Where(BuildContainsExpression<Table1, int>(t => t.CanceledId.Value, identifiers)).Expression;
Expression.Or(expression, transactionsQuery.Expression);
}
But I get the following exception:
The binary operator Or is not defined for the types 'System.Linq.IQueryable1[Table1]' and 'System.Linq.IQueryable1[Table1]'..
Any ideas? -Am I in the right direction?
Thanks,
Nir.
You are appending your OR in the wrong place. What you are doing now is effectively something like this:
(from t in Context.Table1
where identifiers.Contains(t.Id)
select t)
OR
(where identifiers.Contains(t.CanceledId))
The second problem is that the BuildContainsExpression method you use, returns a lambda expression, something that looks like this:
t => t.Id == 1 || t.Id == 2 || ...
You can't change this expression once it's generated. However, that's what you want because you'd like to have something like this:
t => t.Id == 1 || t.Id == 2 || ... || t.CanceledId == 1 || t.CanceledId == 2 || ...
You can't simply take the body of this lambda expression and or it together with another expression because it depends on the parameter t.
So what you can do is the following:
// Overload of BuildContainsExpression.
private static Expression<Func<T, bool>> BuildOtherContainsExpression<T>(
ParameterExpression p, Expression field1, Expression field2, int[] values)
{
var eq1 = values.Select(v => Expression.Equal(field1, Expression.Constant(v)));
var eq2 = values.Select(v => Expression.Equal(field2, Expression.Constant(v)));
var body = eq1.Aggregate((acc, equal) => Expression.Or(acc, equal));
body = eq2.Aggregate(body, (acc, equal) => Expression.Or(acc, equal));
return Expression.Lambda<Func<T, bool>>(body, p);
}
// Create a parameter expression that represents something of type Table1.
var parameter = Expression.Parameter(typeof(Table1), "t");
// Create two field expressions that refer to a field of the parameter.
var idField = Expression.Property(parameter, "Id");
var canceledIdField = Expression.Property(parameter, "CanceledId");
// And finally the call to this method.
query.Where(BuildContainsExpression<Table1>(
parameter, idField, canceledIdField, identifiers));
Your if statement would now look like this:
if (!showCanceled)
{
// Use original version of BuildContainsExpression.
}
else
{
// Create some expressions and use overloaded version of BuildContainsExpression.
}
I know I'm a bit late to the party here - but I think the original code in the original poster's question was 99% right.
The only wrong was that instead of
Expression.Or
it should have been
Expression.OrElse