I'm working on an ASP.NET MVC application with Entity Framework 6 and a SQL Server database.
I'm trying to shuffle the results of a query by adding a SortingCode which I'd like to assign a value based on the current SessionId, so that every time the returned rows are shuffled without affecting the pagination. SortingCode in this attempt is a string, but it can be any type, as long as it allows me to get shuffled results. I have something like this:
var sessionId = Session.SessionID.GetHashCode();
var rnd = new Random(sessionId);
var query = (from l in _context.Adverts
select new AdvertSummary
{
Id = l.Id,
Title = l.Title,
Description = l.Description,
SortingCode = l.Title.OrderBy(x => rnd.Next()).ToString(),
});
The IQueryable result is then converted into a list later on in my code with:
var pagedResults = query.Skip(skip).Take(pageSize).ToList();
The above attempt with the Random class doesn't work, and results in an error
DbExpressionBinding requires an input expression with a collection ResultType
Is there anything that I can do to get shuffled results?
I would suggest to use SqlFunctions.Checksum for such task. SortingCode will be nearly close to the seeded Random.
var sessionId = Session.SessionID;
var query =
from l in _context.Adverts
select new AdvertSummary
{
Id = l.Id,
Title = l.Title,
Description = l.Description,
SortingCode = SqlFunctions.Checksum(sessionId, l.Title)
};
var pagedResults = query
.OrderBy(x => x.SortingCode)
.ThenBy(x => x.Id)
.Skip(skip)
.Take(pageSize)
.ToList();
Related
//Used to get the data I wanted
var MatchResult = (from a in User
join b in UserResult on a.Id equals b.UserId
where a.Status == "Active" select new
{
a.Id,b.Win,b.Lost,b.WinRate
}
//Used to Append the info grouped By UserId
var result = MatchResult.GroupBy(x => x.UserId)
.Select(grp => new { UserId= grp.Key,
UserEmail = grp.Select(x => x.UserEmail ).FirstOrDefault(),
Content = MatchResult.Where(x => x.UserId== grp.Key).ToList() }).ToList();
As the picture shown below, Columns for content is getting a repetitive from userId 1 I try to use read the linq var result in order to sort out the content by Where clause. but turns out I am getting this error message
Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
I did raw SQL query below to select only certain fields from a table.
{
List<CustEmpVM> CustomerVMlist = new List<CustEmpVM>();
var cid = db.Customers.SqlQuery("select SchedDate from Customer where CustID = '#id'").ToList<Customer>();
}
But i keep getting the error of:
System.Data.Entity.Core.EntityCommandExecutionException occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The data reader is incompatible with the specified ALFHomeMovers.Customer. A member of the type, CustID, does not have a corresponding column in the data reader with the same name.
The exception message is pretty straightforward: the query expected to return full entity of Customer table but only SchedDate column returned, hence EF cannot done mapping other omitted columns including CustID.
Assuming Customers is a DbSet<Customer>, try return all fields from Customer instead:
// don't forget to include SqlParameter
var cid = db.Customers.SqlQuery("SELECT * FROM Customer WHERE CustID = #id",
new SqlParameter("id", "[customer_id]")).ToList();
If you want just returning SchedDate column, materialize query results and use Select afterwards:
var cid = db.Customers.SqlQuery("SELECT * FROM Customer WHERE CustID = #id",
new SqlParameter("id", "[customer_id]"))
.AsEnumerable().Select(x => x.SchedDate).ToList();
NB: I think you can construct LINQ based from the SELECT query above:
var cid = (from c in db.Customers
where c.CustID == "[customer_id]"
select c.SchedDate).ToList();
Similar issue:
The data reader is incompatible with the specified Entity Framework
Use below query instead of raw query:
{
List<CustEmpVM> CustomerVMlist = new List<CustEmpVM>();
var cid = db.Customers.Where(w=>w.Id == YOURCUSTOMERID).Select(s=>new Customer{SchedDate = s.SchedDate }).ToList();
}
It will give compile time error rather than run time error.
I'm trying to get a count of occurrences of a value from my database. It's failing.
My effort is
var dc = new Dal.Entities();
var query = (from d in dc.Instruments
where d.Src.ToLower().Contains("other.png")
select new
{
count = d.Src.Count(),
key = d.Src
}
);
This keeps throwing the following exception
"DbExpressionBinding requires an input expression with a collection ResultType.\r\nParameter name: input"
If I change select new... to select d then it works fine so I know that part of the query is OK.
I don't understand why I can't get a Count of each string it finds. What did I do wrong?
edit
If my database is
Src (column title)
my value
my other value
my value
I'm hoping to get the result of
my value, 2
my other value, 1
You need to group by then:
var query = from d in dc.Instruments
where d.Src.ToLower().Contains("other.png")
group d by d.Src into g
select new
{
count = g.Count(),
key = g.Key
};
var items = dc.Instruments
.Where(p => p.Src.ToLower().Contains("other.png"))
.Count();
or
var items = (from item in dc.Instruments
where item.Src.ToLower().Contains("other.png")
select item).Count();
I read many questions on this topic and created the following almost dynamic query:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => new {
movie = Return.As<string>("movie.title")
}).Results;
Unfortunately this isn't dynamic since I'm declaring the movie property in the Return anonymous type.
In all the examples I found the only option is to return the nodes as an object matches the node properties,
like: movie = Return.As<string>("movie.title")
I want the Return statement to give me back a key-value pair list of all the node properties (it can be in any representation like JSON etc..), since
my nodes are generic and not from a specific object kind every time.
is that possible?
You can do something like this:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => Return.As<Node<Dictionary<string,string>>>("movie"));
var results = resQuery.Results.Select(r => r.Data);
Console.WriteLine(results.First()["title"]);
Alternatively, something like:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => Return.As<Node<string>>("movie"));
var results = resQuery.Results;
List<dynamic> nodes = results.Select(r => JsonConvert.DeserializeObject<dynamic>(r.Data)).ToList();
Console.WriteLine(nodes[0].title);
Few days back I put a question regarding mapping two classes Message and MessageStatusHistory using EF. The mapping is going fine but I am facing some problems with the navigation property StatusHistory in class Message that relates it to MessageStatusHistory objects. I am loading the messages for one user only and want to the statuses pertaining to that user only. Like I would want to show if the user has marked message as read/not-read and when. If I use default loading mechanism like following it loads all the history related to the message irrespective of the user:
IDbSet<Message> dbs = _repo.DbSet;
dbs.Include("StatusHistory").Where(x=>x.MessageIdentifier == msgIdentifier);
To filter history for one user only I tried following trick:
IDbSet<Message> dbs = _repo.DbSet;
var q = from m in dbs.Include("StatusHistory")
where m.MessageIdentifier == msgIdentifier
select new Message
{
MessageIdentifier = m.MessageIdentifier,
/*OTHER PROPERTIES*/
StatusHistory = m.StatusHistory
.Where(x => x.UserId == userId).ToList()
};
return q.ToList();//THROWING ERROR ON THIS LINE
I am getting the error:
The entity or complex type 'MyLib.Biz.Message' cannot be constructed in a LINQ
to Entities query.
I have tried by commenting StatusHistory = m.StatusHistory.Where(x => x.UserId == userId).ToList() also but it has not helped.
Please help me in getting Messages with filtered StatusHistory.
EDIT:- above is resolved with this code:
var q = from m in _repository.DBSet.Include("Histories")
where m.MessageIdentifier == id
select new {
m.Id,/*OTHER PROPERTIES*/
Histories = m.Histories.Where(x =>
x.SenderId == userId).ToList()
};
var lst = q.ToList();
return lst.Select(m => new Message{
Id = m.Id, MessageIdentifier = m.MessageIdentifier,
MessageText = m.MessageText, Replies = m.Replies,
ReplyTo = m.ReplyTo, Histories = m.Histories, SenderId =
m.SenderId, SenderName = m.SenderName, CreatedOn = m.CreatedOn
}).ToList();
But if I try to include replies to the message with:
from m in _repository.DBSet.Include("Replies").Include("Histories")
I am getting error on converting query to List with q.ToList() for Histories = m.Histories.Where(x=> x.SenderId == userId).ToList().
About your EDIT part: You cannot use ToList() in a projection, just leave it an IEnumerable<T> and convert to a List<T> when you construct the Message. You also don't need to create two list objects, you can switch from the LINQ to Entities query to LINQ to Objects (the second Select) by using AsEnumerable():
var list = (from m in _repository.DBSet
where m.MessageIdentifier == id
select new {
// ...
Histories = m.Histories.Where(x => x.SenderId == userId)
})
.AsEnumerable() // database query is executed here
.Select(m => new Message {
// ...
Histories = m.Histories.ToList(),
// ...
}).ToList();
return list;
Be aware that Include has no effect when you use a projection with select. You need to make the properties that you want to include part of the projection - as you already did with select new { Histories.....