Tag: linq

生成n-ary Cartesian产品示例

我发现Eric Lippert的post适合我遇到的一个特殊问题。 问题是我无法理解我应该如何使用2个以上的集合。 有 var collections = new List<List>(); foreach(var item in somequery) { collections.Add( new List { new MyType { Id = 1} .. n } ); } 如何在变量集合中应用笛卡尔积linq查询? 扩展方法是这样的: static IEnumerable<IEnumerable> CartesianProduct(this IEnumerable<IEnumerable> sequences) { IEnumerable<IEnumerable> emptyProduct = new[] { Enumerable.Empty()}; return sequences.Aggregate( emptyProduct, (accumulator, sequence) => from accseq in accumulator from item […]

LinqToSql奇怪的行为

我有以下代码: var tagToPosts = (from t2p in dataContext.TagToPosts join t in dataContext.Tags on t2p.TagId equals t.Id select new { t2p.Id, t.Name }); //IQueryable tag2postsToDelete; foreach (Tag tag in tags) { Debug.WriteLine(tag); tagToPosts = tagToPosts.Where(t => t.Name != tag.Name); } IQueryable tagToPostsToDelete = (from t2p in dataContext.TagToPosts join del in tagToPosts on t2p.Id equals del.Id select […]

LINQ to SQL:左连接多个列

首先,我搜索了google / SO,检查了一些示例,但我没有设法编写正确的linq表达式: 这是我的工作SQL查询的样子: select * from Places p left join VoteLog v on p.Id = v.PlaceId and v.UserId = ‘076a11b9-6b14-4230-99fe-28aab078cefb’ –demo userid 这是我对linq的尝试: public IQueryable GetAllPublic(string userId) { var result = (from p in _db.Places join v in _db.VoteLogs on p.Id equals v.PlaceId // This works but doesn’t fully reproduce my SQL query // […]

找到可能的组合linq

我需要在{“a”, “b”,”c”}之间生成所有可能的组合。 例如,输入集如{“a”, “b”,”c”} ,预期输出为{“a”, “b”, “c” “ab”, “ac”, “bc”, “abc”} 。

LINQ表达式转换/从Int到String的Concat

我想用Concat两个表达式作为最终表达式 Expression<Func> 所以我创建了表达式belwo代码只适用于字符串类型,如果我将memberExpression作为Int32或DateTime抛出exception 类型’System.Int32’的表达式不能用于方法’System.String Concat(System.String,System.String)’的’System.String’类型的参数 如果我将表达式转换为 var conversion = Expression.Convert(memberExpression, typeof (string)); 获取没有强制运算符是在类型’System.Int32’和’System.String’之间定义的。 请帮我解决 码 MethodInfo bodyContactMethod = typeof (string).GetMethod(“Concat”,new[] {typeof (string), typeof (string)}); ParameterExpression parameter = Expression.Parameter(typeof (T)); body = Expression.Call(bodyContactMethod, cons, memberExpression); return Expression.Lambda<Func>(body, parameter);

c#使用Tuple对List 进行排序?

我需要按发布日期对MediaItem对象的List 进行排序…发布日期不是项目的属性。 因此,我最初的目的是暂时处理发布日期属性,加载,排序和丢弃属性。 有人在我的工作中建议我使用Tuple并使用LINQ进行排序…我在我的片段测试中得到了这个,填写了List …但我不确定如何使用该日期属性进行排序LINQ。 我会一直看着我,但如果有人得到任何帮助我会很感激。 谢谢。 List<Tuple> list = new List<Tuple>(); for (int i = 0; i <= 10; i++) { DateTime date = new DateTime(2011, i, 1); MediaItem item = new MediaItem(); list.Add(new Tuple(item, date)); }

有没有机会使用Linq(C#)获得独特的记录?

我有一个list<list> 在list[x][0]是我想要选择唯一记录的记录,因此这样的记录不会出现在任何其他list[x][0 ]中,当我选择它时,我想要整行list[x]被选中。 我还没有在Linq找到适当的这个例子,请帮忙:( 编辑 当Jon Skeet要求我澄清时,我不能否认;-) list<list> 包含字符串表的列表。 每个字符串“table”包含几个键list[x][several_items] several list[x][several_items] ,我想从list->中获取唯一记录,这意味着该“表”中的第一项。 从而: item[0] = “2”,”3″,”1″,”3″ item[1] = “2”,”3″,”4″,”2″ item[3] = “10”,”2″ item[4]= “1”,”2″ – > unique意味着我可以将行item[3] and item[4]派生为唯一。 因为第一次出现数字/字符串很重要。 如果列表中有多个记录/行( item[x] of which first item (item[x][0])存在多次,则它不是唯一的。 每个列表的第一个元素对于确定唯一性很重要。 也许如果有人可以帮助找到找到非唯一的方法会更容易 – >所以从上面的例子我只得到项目[0]和项目[1]

LINQ交叉,多个列表,一些空

我正试图找到与LINQ的交叉点。 样品: List int1 = new List() { 1,2 }; List int2 = new List(); List int3 = new List() { 1 }; List int4 = new List() { 1, 2 }; List int5 = new List() { 1 }; 想要返回:1,因为它存在于所有列表中..如果我运行: var intResult= int1 .Intersect(int2) .Intersect(int3) .Intersect(int4) .Intersect(int5).ToList(); 它返回任何内容,因为1显然不在int2列表中。 无论一个列表是否为空,如何使其工作? 使用上面的例子或: List int1 = new […]

如何使用LINQ计算与条件匹配的元素数

我尝试了很多东西,但对我来说最符合逻辑的是这个: int divisor = AllMyControls.Take(p => p.IsActiveUserControlChecked).Count(); AllMyControls是UserControls的集合,我想知道的是有多少UserControls将IsActiveUserControlChecked属性设置为true。 我在VS中得到的是: Cannot convert lambda expression to type ‘int’ because it is not a delegate type 我的表情怎么了?

FindAsync和Include LINQ语句

我到目前为止的代码工作得很好 public async Task Details(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ItemDetailModel model = new ItemDetailModel(); model.Item = await db.Items.FindAsync(id); if (model.Item == null) { return HttpNotFound(); } return View(model); } 但是我想要包含1个表,不能使用FindAsync public async Task Details(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ItemDetailModel model = […]