所以我得到了这段代码:
ClassA { List<ClassB> classBList;}
ClassB { List<ClassC> classCList;}
ClassC { String ID;}
Dictionary<string,int> dict;
ClassA MyObject;
Run Code Online (Sandbox Code Playgroud)
字典上的键应该与ClassC上的ID字段匹配.我想在linq上执行以下查询
List<String> matches = (from b in MyObject
from c in b.classCList
join d in dict
on c.ID equals dict.Key
select new
{
c.Value == 0 ? "YES" : "NO"
}).ToList();
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误: "Invalid anonymous type member ..."
底线是......我怎样才能在select new中有条件?
编辑
如何使用扩展方法执行此查询?
有帮助吗?泰
我试图从csproj文件中获取所有dll名称,但无法得到任何东西!所以,我尝试使用liq查询从ItemGroup标签获取al元素:
var elem = doc.Descendants("Project").Where(t => t.Attribute("ToolsVersion")!=null)
.Elements("ItemGroup").Elements("Reference").Where(r => r.Attribute("Include") != null);
var attrs = elem.Attributes();
Console.WriteLine(attrs.Count());
foreach (var e in attrs)
{
Console.WriteLine(e);
}
Run Code Online (Sandbox Code Playgroud)
这是我的xml csproj文件.我剪了一些不成熟的文字))
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="MyProject1" />
<Reference Include="MyProject2" />
<Reference Include="MyProject3" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
请帮我拿这个名单!我做错了什么? …
我有以下场景:
public JsonResult ChangeFilterList(int option)
{
var data = new[] { new { Text = "Unknown option", Value = -1 } };
switch (option)
{
case 2: data = _departmentnameRepository.All.Select(x => new { Text = x.DeptName, Value = x.Id }).ToArray();
break;
case 3: data = Session["projectid"] == null
? _assetSequenceRepository.All.Select(x => new { Text = x.AssetShotName, Value = x.Id }).ToArray()
: _assetSequenceRepository.FindBy(p => p.ProjectId == (int)Session["projectid"]).Select(x => new { Text = x.AssetShotName, Value = x.Id }).ToArray();
break;
default: data = …Run Code Online (Sandbox Code Playgroud) 我有一个新手问题.
我试图更新表的一行,特别是使用linq使用主键值.
var mytab = new Customer();
mytab.FirstName = mymodel.FirstName;
mytab.LastName = mymodel.LastName;
mytab.CustomerId = mymodel.CustomerId;
db.AddToCustomer(mytab);
db.SaveChanges()
Run Code Online (Sandbox Code Playgroud)
这只是添加了一个带有新id的新行.单步调试代码会发现'mymodel'正在传递正确的值,并带有我要更新的名字或姓氏.
mymodel.CustomerId是我要更新的记录的正确ID,但SQL事件探查器显示我没有更新任何内容.我需要使用.First吗?
这是另一个"LINQ to实体无法识别方法问题"......但是,下面的代码不是从根本上做同样的事情吗?
作品:
var returnData = from x in MyEntities.MyDBSet
where x.MyDBSetPrimaryKey == id
select new Models.MyModelDTO
{
MyPropOne = (int)x.MyModel.MyOtherPropOne,
MyPropTwo = x.MyOtherPropTwo ?? 0,
MyPropThree = x.MyModel.MyOtherPropThree,
MyPropFour = x.MyModel.MyOtherPropFour,
MyPropFive = x.MyModel.Entity.MyOtherPropFive,
MyPropSix = x.MyModel.MyOtherPropSix == null ? 0 : (decimal)x.MyModel.MyOtherPropSix,
MyPropSeven = x.MyModel.SomeType.MyOtherPropSeven,
MyPropEight = (int)x.MyModel.MyOtherPropEight,
MyPropNine = x.MyModel.MyPropNine == null ? 0 : (int)x.MyModel.MyOtherPropNine,
MyPropTen = x.MyModel.MyOtherPropTen == null ? 0 : (int)x.MyModel.MyOtherPropTen,
MyPropEleven = x.OtherEntity.MyOtherPropEleven,
MyPropTwelve = x.MyOtherpropTwelve
};
Run Code Online (Sandbox Code Playgroud)
不起作用:
包含在扩展方法中的完全相同的赋值:
public static MyModelDTO …Run Code Online (Sandbox Code Playgroud) 在我看来,Linq只能查询给定的数据源,也就是说,它浏览它并根据需要从中返回东西,但不会改变任何东西.但是,我在这个问题上找到的一些答案中的措辞让我怀疑这种理解.在所有情况下,我的理解是否正确?linq查询是否可以更改与其关联的数据源的内容?
我目前正在使用LINQ-> Entites w/Entity Framework v 4.2和T4生成的对象上下文遇到问题.我对LINQ查询在针对数据库执行之前如何转换为存储表达式相对了解,但我绝不是专家.据我所知,所使用的.NET函数在T-SQL中转换为它们的等价物,而那些没有等价物的函数(例如String.Format())将System.NotSupportedException在运行时抛出.
在我的动态LINQ查询中,我DateTime.Now.AddMinutes()用来过滤掉我的结果集:
public void ExecuteSearch()
{
var timeElapsedTresholdInMinutes = Convert.ToInt32( ConfigurationManager.AppSettings.Get("TimeElapsedThresholdInMinutes") );
var resultSet = ( from entity in _dbContext.EntityCollection
where /*non-date-related where clause stuff*/
&&
entity.DateAdded <= DateTime.Now.AddMinutes(-1 * timeElapsedThresholdInMinutes)
select entity);
/* do something with resultSet */
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我得到了 System.NotSupportedException: LINQ to Entities does not recognize the method DateTime.Now.AddMinutes() method and this method cannot be translated into a store expression.
比方说,我的timeElapsedThreshold在评估后的值为-30.有谁知道为什么这不会映射到DATEADD(MINUTE,-30,GETDATE());?这里有什么我想念的吗?
当然,我可以将我的代码转换为:
public void ExecuteSearch() …Run Code Online (Sandbox Code Playgroud) 如何在DateTime.Now和特定DateTime之间获得int的天数?可以使用LINQ完成吗?
我最初有一个包含LINQ查询返回的方法,int[]然后以类似于以下的方式使用它:
int[] result = something.Where(s => previousarray.Contains(s.field));
Run Code Online (Sandbox Code Playgroud)
事实证明这是非常缓慢的,直到第一个数组被检索为本机IQueryable<int>.它现在运行得非常快,但我想知道如果我int[]从其他地方提供了一个必须如上所述使用的情况我将如何处理这种情况.
在这种情况下有没有办法加快查询速度?转换为列表似乎没有帮助.
使用LINQ如何将列表列表转换为新的列表列表,其中每个列表都包含来自同一索引的所有元素.
例如
l[0] = {3,15,22,6}
l[1] = {9,81,52,7}
l[2] = {2,0,32,73}
Run Code Online (Sandbox Code Playgroud)
会变成
l[0] = {3,9,2}
l[1] = {15,81,0}
l[2] = {22,52,32}
l[3] = {6,7,73}
Run Code Online (Sandbox Code Playgroud)
谢谢