对于像这样的SQL查询,如何将其转换为linq?
SELECT *
FROM table1 t
WHERE NOT (t.col1 = 1 AND t.col2 = 2)
Run Code Online (Sandbox Code Playgroud) 我有一个大问题.
我是最近5年的SQL-boy,但现在我需要将我的SQL查询转换为LINQ到实体C#格式.因为我现在是LINQ(复杂语句)的新手,我需要快速帮助.
提前致谢.
PS我也需要一些建议,一些起点可以迅速开始学习LINQ实体.
这是我的SQL(直接来自我的应用程序(@ endDate,@ endDate和@glChartID也作为参数保存在我的c#app中)):
SELECT budget.accountid,
budget.perioddate,
budget.lastyear,
budget.thisyear,
budget.budget,
budget.operplan,
budget.forecast,
glchart.accounttype,
glchart.headertype
FROM budget INNER JOIN glchart ON budget.accountid = glchart.id
WHERE budget.accountid = @glChartID AND budget.perioddate BETWEEN @startDate and @endDate AND glchart.headertype NOT LIKE 'Header%'
UNION
SELECT glchart.id,
budget.perioddate,
SUM(ISNULL(budget.lastyear, 0)),
SUM(ISNULL(budget.thisyear, 0)),
SUM(ISNULL(budget.budget, 0)),
SUM(ISNULL(budget.operplan, 0)),
SUM(ISNULL(budget.forecast, 0)),
glchart.accounttype,
glchart.headertype
FROM budget INNER JOIN glchart ON budget.accountid = glchart.id
WHERE budget.accountid
IN
(SELECT g.id FROM glchart g
WHERE g.code >= glchart.code AND g.code …Run Code Online (Sandbox Code Playgroud) 我有一张桌子,你可以在下面看到
表项目
ID Type Sold (bit)
--------------------------------------------
1 Book 1
2 Phone 0
3 TV 1
4 TV 1
5 TV 1
6 TV 0
7 Phone 1
8 Phone 0
Run Code Online (Sandbox Code Playgroud)
我需要按类型分组,计算并计算销售的商品数量.所以我可以得到如下所示的结果
Phone 3; Sold 1
TV 4; Sold 3
Book 1; Sold 1
Run Code Online (Sandbox Code Playgroud) 我正在写一个Linq查询.有没有一种方法可以根据一些if条件连接到查询?
就像查询一样
from res in _db.Person
where res.Departments.ID == deptId
select res;
Run Code Online (Sandbox Code Playgroud)
如果我的条件是真的,我希望它是这样的
from res in _db.Person
where res.Departments.ID == deptId && res.Departments.Type == deptType
select res;
Run Code Online (Sandbox Code Playgroud) 我的查询:
public List<Book> GetAllBook()
{
return (from c in this.LDEntities.Book
select new
{
c.IdBook,
c.NameBook,
c.Athour
}).ToList();
}
Run Code Online (Sandbox Code Playgroud)
错误:
无法将类型'AnonymousType#1'隐式转换为'System.Collections.Generic.List
什么是正确的代码?
我想AddMonths在查询中使用
List<Entities.Subscriber> items = (from s in context.Subscribers
where s.Validated == false && s.ValidationEmailSent == true && s.SubscriptionDateTime < DateTime.Now.AddMonths(-1)
select s).ToList();
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
LINQ to Entities无法识别方法'System.DateTime AddMonths(Int32)'方法,并且此方法无法转换为存储表达式.
有没有办法在我的查询中使用这个功能?
假设我有一个Posts包含字段Owner类型的表Person(有字段Name).
我正在尝试使这个速记表达式工作:
from p in Posts where p.Author == "SomeNameAsString" select p;
Run Code Online (Sandbox Code Playgroud)
这意味着我必须将类型对象Person与a 进行比较string.
我试图覆盖Equals并覆盖运算符==和!=,但我仍然得到异常"DbComparisonExpression需要具有可比类型的参数.".
有没有办法在实体框架中使两种不同类型具有可比性?
我正试图通过Guid(最常用的)订购一堆记录,然后按此列分组,并采用前3个最常用的Guids.
IList<Records> dbobjs = dbContext.Records
.OrderByDescending(t => t.Id)
.GroupBy(t => t.Id)
.Take(3)
.ToList();
Run Code Online (Sandbox Code Playgroud)
这不行......我做错了什么?它的说法是:
Cannot implicitly convert type 'System.Collections.Generic.List<System.Linq.IGrouping<System.Guid,DataAccessLayer.Records>>' to 'System.Collections.Generic.IList<DataAccessLayer.Records>'. An explicit conversion exists (are you missing a cast?)
谢谢
我有这个方法:
public virtual IEnumerable<Invoice> GetHomePageInvoices(IList<Area> areas, FinancialYearLookup financialYear)
{
var homePageInvoices = _db.Invoices.Where(x => areas.Any(z => z.Id == 3)).ToList();
...
}
Run Code Online (Sandbox Code Playgroud)
基本上我正在尝试找到该区域与参数区域中的任何区域匹配的任何发票.
我收到错误:
无法创建"Models.Area"类型的常量值.在此上下文中仅支持原始类型(例如Int32,String和Guid').
任何人都可以解释为什么会发生这种情况以及如何解决?
var sites =
from country in db.Countries
select new SitiesViewByUser()
{
Country = country.Title,
City = country.Cities
.Select(m => m.Title).ToArray()
};
Run Code Online (Sandbox Code Playgroud)
城市 - 数组字符串.
我需要得到数组Cities.Title
这段代码:
foreach(var item in sites)
Run Code Online (Sandbox Code Playgroud)
得到这个错误:
LINQ to Entities的表达式无法识别方法"System.String [] ToArray [String](System.Collections.Generic.IEnumerable` 1 [System.String])",因此无法将其转换为表达式存储.