我试图在这个linq查询中获取其余的选择键但是intellisense给了我一个错误
var query2 = from row2 in query1
group row2 by row2.questionid into g
where g.Count() > 0
select new
{
questionid1, //Error here
time, //Error here
thecount = g.Count()
};
Run Code Online (Sandbox Code Playgroud)
我如何获得这些选择键?
我Unable to cast object of type 'System.Int64' to type 'System.String'.
从下面的代码片段中得到了这个
:
IList<long> Ids = new List<long>();
Ids.Add(6962056);
Ids.Add(7117210);
Ids.Add(13489241);
var stringIds = Ids.Cast<string>().ToArray();
Run Code Online (Sandbox Code Playgroud)
和Booooooooooooom ....想法?
我想在扩展方法的函数中修改局部变量.看到
int myvar=0;
MyList.Where(
x =>
{
if (condition)
myvar += 1;
return false;
});
return myvar;
Run Code Online (Sandbox Code Playgroud)
为什么那不起作用?
您是否看到了更好的方法来获取和连接item.Number在一个字符串中?
当前:
var numbers = new StringBuilder( );
// group is the result of a previous group by
var basenumbers = group.Select( item => item.Number );
basenumbers.Aggregate
(
numbers,
( res, element ) => res.AppendFormat( "{0:00}", element )
);
Run Code Online (Sandbox Code Playgroud) 对于像这样的SQL查询,如何将其转换为linq?
SELECT *
FROM table1 t
WHERE NOT (t.col1 = 1 AND t.col2 = 2)
Run Code Online (Sandbox Code Playgroud) 我有ListAllUsers.用户有propeorty id(作为Guid),name(作为String),address(作为String).我有另一个用户ID列表ID,它们是从ui中选择的.我想获得List selectedUsers,来自AllUsers的ID.我试图通过LINQ获取它.我可以通过单一陈述来做到这一点,没有多次尝试吗?怎么样?
我在代码中看到一个奇怪的行为,这是一个使用苹果和人的类似例子,但代码基本相同:
List<Apple> apples = ...
var selectableApples = apples.Select(a => new SelectableApple { SelectedByPerson = null, Apple = a });
foreach (Person person in persons)
{
foreach (var unselectedApple in selectableApples.Where(aa => aa.SelectedByPerson == null))
{
if (/*the person satisfies some conditions*/)
{
// This gets executed like 100 times:
unselectedApple.SelectedByPerson = person;
}
}
}
foreach (var selectedApple in selectableApples.Where(aa => aa.SelectedByPerson != null))
{
Unreachable code - the collection is empty... WTF???
}
Run Code Online (Sandbox Code Playgroud)
的SelectableApple类是没有逻辑只是一个普通的C#类,以及用于所有属性公共getter和setter.
为什么会这样?
提前致谢!
var ids = new int[] { 3, 2, 20, 1 };
var entities = categories.Where(entity => ids.Contains(entity.Id));
Run Code Online (Sandbox Code Playgroud)
我必须像在ids数组中那样对实体进行完全相同的排序.我怎样才能做到这一点 ?
我从两个不同的存储库中检索数据:
List<F> allFs = fRepository.GetFs().ToList();
List<E> allEs = eRepository.GetEs().ToList();
Run Code Online (Sandbox Code Playgroud)
现在我需要加入他们,所以我做了以下事情:
var EFs = from c in allFs.AsQueryable()
join e in allEs on c.SerialNumber equals e.FSerialNumber
where e.Year == Convert.ToInt32(billingYear) &&
e.Month == Convert.ToInt32(billingMonth)
select new EReport
{
FSerialNumber = c.SerialNumber,
FName = c.Name,
IntCustID = Convert.ToInt32(e.IntCustID),
TotalECases = 0,
TotalPrice = "$0"
};
Run Code Online (Sandbox Code Playgroud)
如何使这个LINQ查询更好,以便它运行得更快?我将不胜感激任何建议.
谢谢
所以我有以下代码:
var itemsGrouped = this.Errors.GroupBy(x => x.UniqueName).AsEnumerable();
var hasErrors = !itemsGrouped.Any((f) =>
{
var errorCount = f.ToArray()
.Where(x => x.ErrorCount.HasValue)
.Count(x => x.ErrorCount.Value > 0);
return errorCount > 2;
});
Run Code Online (Sandbox Code Playgroud)
现在我想检索与.Any()查询匹配的单个项目.我如何只获得匹配的项目?