如何解决Asp.net mvc linq
我有 Item 表和该项目的 Transaction 表。
我的项目表就像
[ID] [Item_Name]
001,苹果
002,香蕉
这是我的交易表
[ID] [成交价格] [成交日期]
001,1 $,2020/3/10
001,1.5 $,2020/04/10
001,2 $,2020/05/10
002,1 $,2020年10月10日
002,1.2 $,2020/04/10
002,1.3 $,2020/3/10
我想得到这样的
苹果以2美元的价格出售。
香蕉以1.3美元的价格售出。
我想知道的是如何获得所售商品的最后价格。我还想加入两个桌子并获得商品的最后价格。
我尝试过
var soldprice = (from i in Item join t in Transaction on i.ID equal t.ID select t).select(x => x.Sold_Price).LastOrDefault().toList();
解决方法
如果您不在EF / EF Core中使用关系,这将是我的方法
migrating hmac-auth on database 'kong'...
hmac-auth migrated up to: 000_base_hmac_auth (executed)
hmac-auth migrated up to: 002_130_to_140 (executed)
hmac-auth migrated up to: 003_200_to_210 (executed)
migrating oauth2 on database 'kong'...
oauth2 migrated up to: 000_base_oauth2 (executed)
oauth2 migrated up to: 003_130_to_140 (executed)
oauth2 migrated up to: 004_200_to_210 (executed)
oauth2 migrated up to: 005_210_to_211 (executed)
migrating ip-restriction on database 'kong'...
ip-restriction migrated up to: 001_200_to_210 (executed)
migrating jwt on database 'kong'...
jwt migrated up to: 000_base_jwt (executed)
jwt migrated up to: 002_130_to_140 (executed)
jwt migrated up to: 003_200_to_210 (executed)
migrating basic-auth on database 'kong'...
basic-auth migrated up to: 000_base_basic_auth (executed)
basic-auth migrated up to: 002_130_to_140 (executed)
basic-auth migrated up to: 003_200_to_210 (executed)
migrating key-auth on database 'kong'...
key-auth migrated up to: 000_base_key_auth (executed)
key-auth migrated up to: 002_130_to_140 (executed)
key-auth migrated up to: 003_200_to_210 (executed)
migrating session on database 'kong'...
session migrated up to: 000_base_session (executed)
migrating acl on database 'kong'...
acl migrated up to: 000_base_acl (executed)
acl migrated up to: 002_130_to_140 (executed)
acl migrated up to: 003_200_to_210 (executed)
acl migrated up to: 004_212_to_213 (executed)
migrating response-ratelimiting on database 'kong'...
response-ratelimiting migrated up to: 000_base_response_rate_limiting (executed)
migrating bot-detection on database 'kong'...
bot-detection migrated up to: 001_200_to_210 (executed)
migrating acme on database 'kong'...
acme migrated up to: 000_base_acme (executed)
38 migrations processed
38 executed
Database is up-to-date
C:\Users\donhuvy>docker run -d --name kong --network=kong-net -e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" -e "KONG_PG_USER=kong" -e "KONG_PG_PASSWORD=kong" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" -e "KONG_PROXY_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_LISTEN=0.0.0.0:8001,0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 127.0.0.1:8001:8001 -p 127.0.0.1:8444:8444 kong:latest
docker: Error response from daemon: Conflict. The container name "/kong" is already in use by container "1326822f2bfefe7b5f5ab76ef630c83127eb7e5661929009c6c0187c0be711ed". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
C:\Users\donhuvy>
在//items = items table
//transactions = transactions table
var results = items.Join(
transactions,item => item.Id,transaction => transaction.Id,(item,transaction) =>
new
{
ItemTransactionId = item.Id,Name = item.Item_Name,Price = transaction.Sold_Price,Date = transaction.Sold_Date
}).GroupBy(x => x.ItemTransactionId)
.Select(x =>
new
{
ResultDesired =
x.Select(r => r.Name).FirstOrDefault() +
" was sold at " +
x.Select(r => new {TransactionPrice = r.Price,Date = r.Date})
.OrderByDescending(ex => ex.Date).First().TransactionPrice
}).ToList();
表之后,我将joining
项/事务ID,然后根据日期重新排列
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。