说我有下一个序列:
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud)
我需要与Linq做下一件事:
1,2;
2,3;
3,4;
4,5;
...
9,10;
Run Code Online (Sandbox Code Playgroud)
无法得到它.
以下内容未在SQL中生成where子句.
search.Where我在调试时正在执行这些部分,但在查看SQL事件探查器时却没有出现.
using (var db = new CERTContext())
{
var search = db.Project.Include("ValueStream").Include("Function").AsQueryable();
if (!lookForDate)
{
search.Where(p => p.ProjectName.Contains(searchText) ||
p.ProjectDescription.Contains(searchText) ||
p.EquipmentName.Contains(searchText) ||
p.ValueStream.Description.Contains(searchText) ||
p.ValueStream.Description.Contains(searchText) ||
p.TrackingId.Contains(searchText) ||
p.ENumber.Contains(searchText) ||
p.ShamrockNumber.Contains(searchText));
//.Take(10)
//.OrderByDescending(p => p.ProjectID);
//.ToList();
}
else
{// Search on date
search.Where(p =>
(System.Data.Objects.EntityFunctions.TruncateTime(p.StartDate) == searchDate));
//.Take(10)
//.OrderByDescending(p => p.ProjectID);
//.ToList();
}
if (showDeleted != true)
{
search.Where(p => p.Deleted == false);
}
pList = search.Take(10).OrderByDescending(p => p.ProjectID).ToList();
}// using (var db = new CERTContext())
Run Code Online (Sandbox Code Playgroud)
这就是我得到的.它有选择前10位,orderby和连接但没有位置. …
我正在使用Razor中的LINQ to Entities.我有这样的事情:
@foreach(Org o in Model) {
@foreach(User u in o.Users.Where(x => x.Active==true) ) {
<span> @u.FirstName </span>
}
}
Run Code Online (Sandbox Code Playgroud)
当我查看SQL Server Profiler时,查询有两个主要的低效率:
当然它工作正常,但我的印象是LINQ比这更聪明,并会向SQL服务器发送更具体的查询.
问题:我可以让LINQ传递WHERE子句(Active == true)并且只能自动获取FirstName列吗?
ps除非严格相关,否则我对于视图中的LINQ是否是良好实践并不感兴趣 - 例如,如果视图的优化程度与编译代码不同?
这是我的控制器代码:
public ActionResult Index()
{
xyz_Entities db = new xyz_Entities();
return PartialView(db.Orgs);
}
Run Code Online (Sandbox Code Playgroud)
更新 - 很多道歉,这实际上是一个嵌套循环,可能是重要的.
例如,我需要从字符串中取第一个数字
"12345 this is a number " => "12345"
"123 <br /> this is also numb 2" => "123"
Run Code Online (Sandbox Code Playgroud)
等等
因为我使用C#代码:
string number = "";
foreach(char c in ebayOrderId)
{
if (char.IsDigit(c))
{
number += c;
}
else
{
break;
}
}
return number;
Run Code Online (Sandbox Code Playgroud)
如何通过LINQ做到这一点?
谢谢!
我有这个问题:
var sole = SoleService.All().Where(c => c.Status != 250)
.Select(x => new {
ID = x.ID, Code = new {CodeName = x.Code + x.Country},
Name = x.Name
})
.Distinct()
.OrderBy(s => s.Code);
Run Code Online (Sandbox Code Playgroud)
像这样它给我一个错误.我想要的是组合Code和Country喜欢concat字符串,所以我可以使用新的变量作为我的数据源.就像是 -001France
PS
我正在使用并正在运行的是:
var sole = SoleService.All().Where(c => c.Status != 250)
.Select(x => new {
ID = x.ID, Code = x.Code, Name = x.Name, Country = x.Country })
.Distinct()
.OrderBy(s => s.Code);
Run Code Online (Sandbox Code Playgroud)
所以我需要修改这个查询,以便我可以Code +Country用作一个变量.以上是我认为可行的尝试.
所以我试着按照这个例子在这个LINQ查询的where子句中有一个子查询.
var innerquery =
from app in context.applications
select new { app.app_id };
IEnumerable<postDatedCheque> _entityList = context.postDatedCheques
.Where(e => innerquery.Contains(e.appSancAdvice.application.app_id));
Run Code Online (Sandbox Code Playgroud)
其目的是选择那些记录postDatedCheques已APP_ID在应用程序表.
但是我在where子句中跟踪了错误:
我的编码错误是什么?
我已经知道你不能在带有LINQ的where子句中使用数组元素.例如:
Department department = db.Departments.Single(d => d.DepartmentID == teams[i].DepartmentID);
Run Code Online (Sandbox Code Playgroud)
那失败了.但是,我很好奇为什么在编译时没有捕到它?是否存在这样的情况,这可以防止这种声明的编译时间错误?
我需要使用Linq to Entities获得前10名记录.
我需要将返回的结果绑定到GridView如下"
Product Name | Product Description | Number of Items Sold
Item 1 | Item 1 Description | 24
Run Code Online (Sandbox Code Playgroud)
如上所述,物品需要按顺序存储,从销售的大多数商品开始.
以下是我到目前为止所尝试的内容:
public IQueryable GetTopTenProducts(DateTime startDate, DateTime endDate)
{
return
(from p in this.Entities.Product
join pro in this.Entities.ProductOrder on p.ID equals pro.ProductID
join o in this.Entities.Order on pro.OrderID equals o.ID
where o.DateOfOrder >= startDate && o.DateOfOrder <= endDate
select new
{
Product = p,
Quantity = pro.Qty,
ProductName = …Run Code Online (Sandbox Code Playgroud) 我想在linq中的连接谓词中使用两个字段.如何添加其他字段?
var Result = from Period in context.PeriodCosts
join Name in context.Name on Period.ID equals Name.PeriodID
/*"Problem is here ==> */and
Period.CCID equals Name.CCID"
select Name;
Run Code Online (Sandbox Code Playgroud) 我的代码如下:
void Main()
{
var q = from a in Applicants
where(a.Claims.Any())
select a.Claims.Sum(c => c.TotalClaimAmount());
q.Dump();
}
public static class MyExt
{
public static decimal TotalClaimAmount(this Claim c)
{
var t = c.Accommodations.Sum(a => a.AmountClaimed) +
c.MealAllowances.Sum(ma => ma.AmountClaimed) +
c.Meals.Sum(m => m.AmountClaimed) +
c.Mileages.Sum(mi => mi.AmountClaimed) +
c.Others.Sum(o => o.AmountClaimed) +
c.ParkingTransits.Sum(pt => pt.AmountClaimed) +
c.Travels.Sum(tr => tr.AmountClaimed);
return (decimal)t;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在LinqPad中运行时得到以下问题:
InvalidOperationException:无法将表达式'a.Claims.Sum(c => c.TotalClaimAmount())'转换为SQL,并且无法将其视为本地表达式.
请帮帮我.非常感谢
linq-to-entities ×10
c# ×9
linq ×9
.net ×2
linq-to-sql ×2
asp.net ×1
asp.net-mvc ×1
c#-4.0 ×1
linqpad ×1