在阅读Jon Skeet的" Odd query expressions "后,我尝试了下面的代码.我期望最后的LINQ查询转换为int query = proxy.Where(x => x).Select(x => x);不编译因为Where返回一个int.代码编译并将"Where(x => x)"打印到屏幕并将查询设置为2.从不调用Select,但需要在那里编译代码.怎么了?
using System;
using System.Linq.Expressions;
public class LinqProxy
{
public Func<Expression<Func<string,string>>,int> Select { get; set; }
public Func<Expression<Func<string,string>>,int> Where { get; set; }
}
class Test
{
static void Main()
{
LinqProxy proxy = new LinqProxy();
proxy.Select = exp =>
{
Console.WriteLine("Select({0})", exp);
return 1;
};
proxy.Where = exp =>
{
Console.WriteLine("Where({0})", exp);
return 2;
};
int query = from …Run Code Online (Sandbox Code Playgroud) 你会如何使用LINQ查询语法编写这个完全相同的查询?
var q2 = list.GroupBy(x => x.GroupId)
.Select(g => g
.OrderByDescending(x => x.Date)
.FirstOrDefault());
Run Code Online (Sandbox Code Playgroud)
我虽然这应该工作,但它没有:
var q1 = from x in list
group x by x.GroupId into g
from y in g
orderby y.Date descending
select g.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
如果你想玩它,这是一个测试程序:
public class MyClass
{
public int Id { get; set; }
public string GroupId { get; set; }
public DateTime Date { get; set; }
public override bool Equals(object obj)
{
var item = obj as MyClass;
return item == …Run Code Online (Sandbox Code Playgroud) 我的数据库中有一个多对多关系设置,如下所示:
User
-------
Id (PK, Identity)
First
Last
...various other fields
Skill
-------
Id (PK, Identity)
Description
UserSkill
-----------
UserId (PK, FK on User.Id)
SkillId (PK, FK On Skill.Id)
Run Code Online (Sandbox Code Playgroud)
当我在 DbContext 上运行此 LINQ 查询时:
from u in Users
from s in u.Skills
where s.Id == 5
select new
{
u.Id,
s.Description
})
Run Code Online (Sandbox Code Playgroud)
生成的 SQL 包含所有内部联接,这就是我想要的:
SELECT
[Extent1].[UserId] AS [UserId],
[Extent2].[Description] AS [Description]
FROM [dbo].[UserSkill] AS [Extent1]
INNER JOIN [dbo].[Skill] AS [Extent2] ON [Extent1].[SkillId] = [Extent2].[Id]
WHERE 5 = [Extent2].[Id]
Run Code Online (Sandbox Code Playgroud)
但是,当我添加一个简单的额外 …
c# linq linq-to-entities linq-query-syntax entity-framework-5
我有一个场景,我需要加入两个表:
一种
|---------------------|------------------|
| ID | Name |
|---------------------|------------------|
| 1 | John |
|---------------------|------------------|
| 2 | Matt |
|---------------------|------------------|
| 3 | Emma |
|---------------------|------------------|
Run Code Online (Sandbox Code Playgroud)
乙
|---------------------|------------------|
| ID | Text |
|---------------------|------------------|
| 1 | blah blah John |
|---------------------|------------------|
| 2 | this is some data|
|---------------------|------------------|
| 3 | My name is Jeff |
|---------------------|------------------|
Run Code Online (Sandbox Code Playgroud)
我需要使用 LINQ 的查询语法来连接这两个表。
左表需要是表A。
虽然我需要根据“文本”列是否包含表 A 中“名称”列中的文本来加入。
代码应如下所示:
var result = from ta in A
join tb in B on …Run Code Online (Sandbox Code Playgroud) 下面的代码提供了两种方法,它们生成总和小于100的整数对,并且它们根据它们与(0,0)的距离按降序排列.
//approach 1
private static IEnumerable<Tuple<int,int>> ProduceIndices3()
{
var storage = new List<Tuple<int, int>>();
for (int x = 0; x < 100; x++)
{
for (int y = 0; y < 100; y++)
{
if (x + y < 100)
storage.Add(Tuple.Create(x, y));
}
}
storage.Sort((p1,p2) =>
(p2.Item1 * p2.Item1 +
p2.Item2 * p2.Item2).CompareTo(
p1.Item1 * p1.Item1 +
p1.Item2 * p1.Item2));
return storage;
}
//approach 2
private static IEnumerable<Tuple<int, int>> QueryIndices3()
{
return from x in Enumerable.Range(0, 100)
from y in …Run Code Online (Sandbox Code Playgroud) I'm currently trying to make a Web Api with EF Core, and i'm running into some problems joining the tables i've got together. I'm working with the following Database Diagram:

And the data i'm currently getting back from my API looks this this:
[
{
"postId":1,
"postDate":"2018-10-21T21:56:43.9838536",
"content":"First entry in posts!",
"user":{
"userId":1,
"creationDate":"2018-10-21T21:56:36.3539549",
"name":"Hansel"
},
"comments":[
{
"commentId":1,
"postDate":"0001-01-01T00:00:00",
"content":"Nice!",
"user":null
},
{
"commentId":2,
"postDate":"0001-01-01T00:00:00",
"content":"Cool, here's another comment",
"user":null
},
{
"commentId":3,
"postDate":"0001-01-01T00:00:00",
"content":"and the last one for …Run Code Online (Sandbox Code Playgroud) linq linq-query-syntax entity-framework-core asp.net-core-webapi
我有这个非常奇怪的问题,我无法理解.也许有人可以指出我做错了什么.
基本上,我只是尝试使用Linq搜索项目到Sitecore.
所以,我的班级看起来像(我也在使用玻璃)
[SitecoreType(TemplateId = "{TEMPLATE_GIUD}")]
public class MyMappedClass : SharedFieldClass
{
[SitecoreField(FieldName = "mylist")]
public virtual IEnumerable<SharedFieldClass> MyMultilistField { get; set; }
[SitecoreField(FieldName = "field1")]
[IndexField("field1")]
public virtual MyKeyValue field1 { get; set; }
}
[SitecoreType]
public class MyKeyValue
{
public virtual Sitecore.Data.ID Id {get;set;}
public virtual string MyValue{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我执行以下查询时,它可以正常工作.
var results = context.GetQueryable<SearchResultItem>()
.Where(c => ((string)c["field1"]) == "{GUID}").GetResults();
Run Code Online (Sandbox Code Playgroud)
但是,当我执行以下操作时,它返回0结果.
List<MyMappedClass> results = context.GetQueryable<MyMappedClass>()
.Where(c => c.field1.MyValue == "{GUID}").ToList();
Run Code Online (Sandbox Code Playgroud)
我看过这个链接.我已经按照此处描述的第二个过程使用Glass来使用Sitecore7 Search("SharedFieldClass"包含所有基本索引字段).
这是一个非常明显的场景,我相信很多人已经做过了,我在做一些愚蠢的事情.
提前致谢.
/ …
我有一个查询,它检索一堆商店的销售额,并将它们作为列表返回。在我的报告请求屏幕上,用户可以按多个 id 进行过滤 - ShopOwnerId、ShopRegionId、ShopTypeCode 等。
我的查询的结构是获取选定日期之间的所有销售额,然后根据选择进行过滤。这显然是非常低效的。:
private List<Sales> GetFilteredListOfSales(Request reportreq)
{
ModelContainer ctn = new ModelContainer();
List<ShopSale> shopsSales = new List<shopsale>();
// If no filters are selected
//
if (reportreq.RegionalId == null && reportreq.OwnerId == null && reportreq.ShopTypeCode == null)
{
shopsSales = (from sale in ctn.ShopSales
where sale.DateSold >= reportreq.FromDate && sale.DateSold <= reportreq.ToDate
select sale).ToList();
}
// If the regional ID has a value...
//
if (reportreq.RegionalId.HasValue)
{
shopsSales = (from sale in ctn.ShopSales
where sale.Shop.Owner.RegionalId == …Run Code Online (Sandbox Code Playgroud) 我知道这被问了很多次,我在网上搜索了大部分解决方案,但似乎没有任何东西可以帮我.我有一个这种结构的表:
ID | ScheduleId | Filename | Description
1 | 10 | | ....
2 | 10 | test.txt | .....
Run Code Online (Sandbox Code Playgroud)
我希望Filename通过传递ScheduleId 来获取最后一个非空(例如,在这种情况下获取"test.txt").
我尝试了很多东西,似乎没有任何东西让我得到文件名.这是最后一个:
var tempFileName = objContext.SchedulesAndFiles
.Where(x => x.ScheduleId == scheduleId)
.OrderByDescending(x => x.ScheduleId)
.Take(1).Select(x => x.Filename);
Run Code Online (Sandbox Code Playgroud)
这不起作用,虽然我明白为什么它不:
var tempFileName = from e in objContext.SchedulesAndFiles
where e.ScheduleId == scheduleId
orderby e.ScheduleId descending
select e.Filename;
Run Code Online (Sandbox Code Playgroud)
调用.Last()或.LastOrDefault()抛出异常(The query operator 'LastOrDefault' is not supported.)
我有一个任务,我必须加入两个相同类型的列表(客户).他们有类似的条目,我必须避免重复.
这是我的客户类:
class Customer
{
private String _fName, _lName;
private int _age, _cusIndex;
private float _expenses;
public Customer(String fName, String lName, int age, float expenses, int cusIndex)
{
this._fName = fName;
this._lName = lName;
this._age = age;
this._expenses = expenses;
this._cusIndex = cusIndex;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我有两个List<Customer>名字customers1和customers2.我需要在不使用Collections方法的情况下加入这两个方法(比如customer1.Union(customer2).ToList();使用Linq查询).
这是我写的Linq查询:
var joined = (from c1 in customers1
join c2 in customers2
on c1.CusIndex equals c2.CusIndex
select new {c1, c2});
Run Code Online (Sandbox Code Playgroud)
但是这给了我出现在两个列表上的成员.但我需要所有人,而不是重复.有什么办法吗?
linq ×9
c# ×8
join ×2
.net ×1
glass-mapper ×1
list ×1
loops ×1
performance ×1
sitecore ×1
sitecore7 ×1