MVC - how to retrieve multiple records in a controller - sql-server

I have the following action:
public ActionResult Details(string id)
{
MyRecordContext rc = new MyRecordContext();
MyRecord r = rc.MyRecords.Single(x => x.RecordID == _id);
return View(r);
}
But turns out there are multiple records with the same id (table's primary key is a composite key). So I need to retrieve a List of type MyRecord, so I changed the code to:
public ActionResult Details(string id)
{
MyRecordContext rc = new MyRecordContext();
List<MyRecord> rl = rc.MyRecords.Any(x => x.RecordID == id);
return View(rl);
}
But the above is clearly incorrect since method Any returns bool. Can someone help me correct my code?

public ActionResult Details(String id)
{
MyRecordContext rc = new MyRecordContext();
List<MyRecord> rl = rc.MyRecords.Where(x => x.RecordID == id).ToList();
return View(rl);
}
That will return all matches with RecordID == id, then pass this list off to your view. Just make sure you update the Details view as well to accept List<MyRecord> instead of MyRecord (now that you're passing a collection).

In Linq, Any just returns a true/false is any of the values match. You are looking for a simple Where:
List<MyRecord> rl = rc.MyRecords.Where(x => x.RecordID == id);

Related

How can I get an item by a string key using web api?

I can't figure out how to hit an endpoint like "api/GetItems/AB123" (AB123 of course being a string) and have it return that item from my data set. I read the docs on the FindAsync() method and it seemed to indicate that it would accept a string by default. Is there something I need to do to 'id' before passing it into FindAsync()? My DB does not have a primary key, if that matters. (I can't change that either, this is legacy data and I have no control over schema)
My db doesn't have a PK ID field. I need to do the next best thing and target a unique string field.
My GET method:
// GET: api/Items/5
[HttpGet("{id}")]
public async Task<ActionResult<Item>> GetItem(string id)
{
var item = await _context.Items.FindAsync(id); // Error happens here: "InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int64'."
if (item == null)
{
return NotFound();
}
return item;
}
Relevant from my model:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string ItemId { get; set; }
Hello and welcome to the Stack Overflow Community!!
Instead of find you could do .SingleAsync() like the below.
You could also do a .Where(x => x.ItemId == id).SingleAsync(). But this is up to you.
// GET: api/Items/5
[HttpGet("{id}")]
public async Task<ActionResult<Item>> GetItem(string id)
{
var item = await _context.Items.SingleAsync(x => x.ItemId == id);
if (item == null)
{
return NotFound();
}
return item;
}
From your error it is obvious that int is expected in FindById method. Can you check the field type in database but from your model I would say that you don't have correct type.
String can't be used as Identity in this way because SQL Server doesn't know how to generate value for that.
You can check this post for more details on that: DatabaseGeneratedOption.Identity not generating an Id
So to conclude, you should check what do you really have in the database to determine is your model wrong.
If that is not the case and you do have a string in the db you should just retrieve item by using SingleAsync method (note that it will throw exception if the id is wrong).
var item = await _context.Items.SingleAsync(e => e.ItemId == id);
If you don't want an exception if the id doesn't exist you can use:
var item = await _context.Items.SingleOrDefaultAsync(e => e.ItemId == id);
which will return null for non existent id.

NHibernate Convert query to async query

I'm looking at async-ifying some of our existing code. Unfortunately my experience with NHibernate is lacking. Most of the NHibernate stuff has been easy, considering NHibernate 5 has a lot of support for async. I am, however, stuck.
Originally, we do something like this using our Dependency Injection:
private readonly IRepository repository;
public MovieRepository(IRepository repository)
{
this.repository = repository;
}
public Movie Get(int id)
{
return (from movie in repository.Query<Movie>()
select new Movie
{
ID = movie.ID,
Title = movie.Title,
Genre = new Genre
{
ID = movie.Genre.ID,
Name = movie.Genre.Name,
},
MaleLead = movie.MaleLead,
FemaleLead = movie.FemaleLead,
}).FirstOrDefault();
}
//Repository Query method in Repository.cs
public IQueryable<TEntity> Query<TEntity>() where TEntity : OurEntity
{
session = session.OpenSession();
return from entity in session.Query<TEntity>() select entity;
}
This works great for our current uses. We write things this way to maintain control over our queries, especially related to more complex objects, ensuring we get back exactly what we need.
I've tried a few things, like making the Query method return a Task< List< TEntity>> and using the ToListAsync() method, however because I am returning it as that kind of list I cannot query on it.
I'm sure I've missed something. If anyone can help me out, I would appreciate it.
You need to use FirstOrDefaultAsync in this case.
public async Task<Movie> Get(int id)
{
return await (from movie in repository.Query<Movie>()
select new Movie
{
ID = movie.ID,
Title = movie.Title,
Genre = new Genre
{
ID = movie.Genre.ID,
Name = movie.Genre.Name,
},
MaleLead = movie.MaleLead,
FemaleLead = movie.FemaleLead,
}).FirstOrDefaultAsync();
}
Add this using statement to your file
using NHibernate.Linq;
Then you can change your method to
public async Task<Movie> Get(int id)
{
return await (from movie in repository.Query<Movie>()
select new Movie
{
ID = movie.ID,
Title = movie.Title,
Genre = new Genre
{
ID = movie.Genre.ID,
Name = movie.Genre.Name,
},
MaleLead = movie.MaleLead,
FemaleLead = movie.FemaleLead,
}).FirstOrDefaultAsync();
}
NB: This is only available from NHibernate 5
Addendum:
The code you have in Repository.cs can be simplified to something like this:
//Repository Query method in Repository.cs
public IQueryable<TEntity> Query<TEntity>() where TEntity : OurEntity
{
//session = session.OpenSession(); //this is obviously wrong, but it's beside the point
var session = sessionFactory.OpenSession();
return session.Query<TEntity>(); //the fix
}

EF Core Accessing Nested-nested Entity

I have an entity CallTrackerLog which has many Clients which have a one-many Advices. I am trying to HttpPost a create for the advice:
[HttpPost("{callTrackerId}/{clientId}/advice")]
public IActionResult CreateCTClientAdvice(int callTrackerId, int clientId,
[FromBody] CallTrackerClientAdvice newAdvice)
{
if (newAdvice == null)
return BadRequest();
if (!ModelState.IsValid)
return BadRequest(ModelState);
var ctFromStore = _context.CallTrackers
.Include(log => log.CallTrackerClients)
.ThenInclude(log => log.CallTrackerClientAdvice)
.FirstOrDefault(ct => ct.CallTrackerId == callTrackerId);
var ctAdviceFromStore ctFromStore.CallTrackerClients.CallTrackerClientAdvice
.FirstOrDefault(c => c.CallTrackerClientId == clientId);
// ... add to db
return Ok();
}
The problem is that I cannot access the CallTrackerClientAdvice with the .FirstOrDefault(ct => ct.CallTrackerClientId == clientId) - it gives me a red underline even though I thought I loaded it above.
The error:
How come I am unable to access the CallTrackerClientAdvice?
I suspect that what you want is:
var ctAdviceFromStore = ctFromStore.CallTrackerClients
.FirstOrDefault(c => c.CallTrackerClientId == clientId)?.CallTrackerClientAdvice;

Dapper - QueryMultiple - 3 tables

I had this relation:
How to retrieve the information in an order entity and invoice entity with a QueryMultiple entity ?
Thanks
QueryMultiple is used when you are accessing multiple result sets, i.e. multiple select, as in:
select * from Order where Id=#id
select * from Invoice where Id = (...probably some sub-query)
At the moment, there is no inbuilt API to stitch this type of query together; instead you would do something like:
using(var multi = conn.QueryMultiple(...)) {
var order = multi.ReadSingle<Order>();
order.Invoice = multi.ReadSingleOrDefault<Invoice>(); // could be null if 0 rows
return order;
}
I would like to add an improved API for this scenario, but it is very awkward to express "join this to that using this property as the association, where {this}.{SomeMember} equals {that}.{SomeOtherMember}".
However, if you are actually doing a single query, as in:
select o.*, i.*
from Order o
left outer join Link l on ...
left outer join Invoice i on ...
where o.Id = #id
then you can use the various Query<,...,> overloads; for example:
int id = ...
var order = conn.Query<Order, Invoice, Order>(sql,
(x,y) => {x.Invoice = y; return x;}, args: new { id }, splitOn: "NumOrder").Single();
Generic code for three tables:
public static Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>> ExecuteQueryMultiple<T1, T2, T3>(string sql, object parameters,
Func<GridReader, IEnumerable<T1>> func1,
Func<GridReader, IEnumerable<T2>> func2,
Func<GridReader, IEnumerable<T3>> func3)
{
var objs = getMultiple(sql, parameters, func1, func2, func3);
return Tuple.Create(objs[0] as IEnumerable<T1>, objs[1] as IEnumerable<T2>, objs[2] as IEnumerable<T3>);
}
private static List<object> getMultiple(string procedureName, object param, params Func<GridReader, object>[] readerFuncs)
{
var returnResults = new List<object>();
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
var gridReader = sqlCon.QueryMultiple(procedureName, param, commandType: CommandType.StoredProcedure);
foreach (var readerFunc in readerFuncs)
{
var obj = readerFunc(gridReader);
returnResults.Add(obj);
}
}
return returnResults;
}
Controller:
[HttpPost]
public ActionResult GetCommodityDetails(int ID)
{
var data = new List<Commodity>();
DynamicParameters param = new DynamicParameters();
param.Add("#ATTRIBUTETYPE", "Your parameter");
param.Add("#CID", Your parameter);
var result = DapperORM.ExecuteQueryMultiple("Store procedure name", param, gr => gr.Read<order>(), gr => gr.Read<Invoice>(), gr => gr.Read<Link>());
return Json(result, JsonRequestBehavior.AllowGet);
}
You can use this concept. It worked for me

How to return List<int> from domain service

Hello guys im using WCF RIA Services i have domain services where i wrote this method
public List<int> GetActionIDs()
{
return (from d in ObjectContext.actions select d.id).ToList();
}
How i can get this List in client side?
This does not works :
List<int> = db.GetActionIDs();
any suggestions?
First of all, you should read the RIA Services manual, because you don't realize that service calls in Silverlight are asynchronous.
In your case, you should
Add InvokeAttribute to your operation in the service:
[Invoke]
public List<int> GetActionIDs()
{
return (from d in ObjectContext.actions select d.id).ToList();
}
Then, all calls to DomainContext are asynchronous, so you get your results in the callback:
db.GetActionIDs(operation =>
{
//TODO: check the operation object for errors or cancellation
var ids = operation.Value; // here's your value
//TODO: add the code that performs further actions
}
, null);
inside DomainSrvice
[Query]
public List<Action> GetActionIDs()
{
List<Action> result = (
from a in ObjectContext.actions
select new action
{
ID = a.ID
}
).ToList();
return result ;
}
Silverlight
DomainService1 DS = new DomainService1();
LoadOperation<Action> LoadOp = Ds.Load(Ds.GetActionIDsQuery());
LoadOperation.Completed += new EventHandler((s,e)=>{
foreach (Action item in LoadOp.Entities)
{
}
});

Resources