Tag: linq

linq中的左外连接

我有以下查询,但我不知道如何在表1上进行左外连接。 var query = (from r in table1 join f in table2 on r.ID equals f.ID select new { r.ID, r.FirstName, r.LastName, FirstNameOnRecord = (f != null ? f.FirstName : string.Empty), LastNameOnRecord = (f != null ? f.LastName : string.Empty), NameChanged = (f != null ? (f.FirstName.CompareTo(r.FirstName) == 0 && f.LastName.CompareTo(r.LastName) == 0) : false) […]

针对ApplicationUser Roles导航属性的ASP MVC Build Throwing Warning?

我有以下ApplicationUser模型: public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection Roles { get; set; } public bool HasRole(string _role) { return Roles.Any(r => r.Name == _role); } public bool HasPermission(string _permission) { return Roles.Any(r => r.Permissions .Any(p => p.Name == _permission)); […]

文件上传读取到内存并用作文本文件 – 有更好的方法吗?

我有一个Intranet托管的Web应用程序,用户将上传一个包含5列空格分隔数据的文本文件。 我不想保存文件,所以我想在内存中使用它。 我在网上尝试了许多不同的例子,但都没有。 最后,一位同事告诉我如何做到这一点。 这是代码,我想知道是否有更好的方法来做到这一点。 最后,我想要的是一种将数据链接到gridview或转发器以便查看和以后存储到数据库(SQL Server)的方法。 上传文件asp标签ID是SurveyFileUpload SurveyDate是一个asp:输入字段 Int32 fileLen = SurveyFileUpload.PostedFile.ContentLength; // Create a byte array to hold the contents of the file. Byte[] buffer = new Byte[fileLen]; // Initialize the stream to read the uploaded file. Stream s = SurveyFileUpload.FileContent; // Read the file into the byte array. s.Read(buffer, 0, fileLen); // […]

无法编辑填充LINQ查询结果的DataGridView

当我使用linq-to-xml查询的结果来填充datagridview时,我无法编辑datagridview。 我已经尝试将datagridview的readonly属性设置为false,这没有用。 我还为cellBeginEdit添加了一个事件处理程序,并在那里放置一个断点,但它没有被击中。 知道我做错了什么,或者这是不可能的? public class MergeEntry { public string author { get; set; } public string message { get; set; } } … var query = from entry in xmlDoc.Descendants(“entry”) select new MergeEntry { author = entry.Element(“author”).Value, message = entry.Element(“msg”).Value, } var queryAsList = query.ToList(); myBindingSource.DataSource = queryAsList; myDataGridView.DataSource = myBindingSource;

Linq-to-Sql:递归获取子项

我有一个Comment表,它有一个CommentID和一个ParentCommentID。 我想获得评论中所有孩子的清单。 这是我到目前为止,我还没有测试过。 private List searchedCommentIDs = new List(); // searchedCommentIDs is a list of already yielded comments stored // so that malformed data does not result in an infinite loop. public IEnumerable GetReplies(int commentID) { var db = new DataClassesDataContext(); var replies = db.Comments .Where(c => c.ParentCommentID == commentID && !searchedCommentIDs.Contains(commentID)); foreach (Comment reply […]

首先是代码:获取没有dataannotations的实体表名

有没有办法用DbModelBuilder定义表信息? 就像是: entity.GetType().GetTableName() 马克斯 编辑: id喜欢实现以下内容 public static class Helper { public string GetTableName(Type type) { // ?? } } 现在我想按类型获取表名 var type = someEntity.getType(); var sql = “delete from ” + Helper.GetTableName(type) + ” where id in (…)”

为什么我会收到InvalidCastException?

我在c#中有以下代码片段 List list = new List() { 1, 23, 5, 3, 423, 3 }; var query = list.Cast().Select(d => d); try { foreach (var item in query) { Console.WriteLine(item); } } catch (Exception ex) { Console.WriteLine(ex.Message); } 它编译得很完美,但是当我执行它时,我得到了exception。

Usinq Linq选择半逗号分隔字符串中的项目?

我有一个半逗号分隔名称的字符串: string names = “Jane;Harry”; 我还有一个客户对象列表: public class Customer { public string FirstName { get; set; } public string LastName { get; set; } } List customers = new List(); customers.Add(new Customer(){FirstName=”John”, LastName=”Doe”}); customers.Add(new Customer(){FirstName=”Jane”, LastName=”Doe”}); customers.Add(new Customer(){FirstName=”Harry”, LastName=”Potter”}); var query = from c in customers select c; 如何创建仅返回名称位于半逗号分隔列表中的客户的查询? 类似于T-SQL SELECT FistName FROM customer WHERE […]

Generic Linq OrderBy函数的问题

我在post中看到了以下函数,它允许用户使用generics表达式对数据进行排序: public static IOrderedQueryable OrderBy( this IQueryable source, Expression<Func> func, bool isDescending) { return isDescending ? source.OrderByDescending(func) : source.OrderBy(func); } 当我尝试使用这个函数时,我得到一个错误,说“找不到类型或命名空间名称”TKey’(你错过了使用指令或程序集引用吗?)“。我在这里做了一些蠢事,但我可以弄清楚。 编辑: 在做了一些研究之后,我认为我的问题在于构建我传递给它的Expression。 是否可以构建一个可以包含不同类型的表达式? 假设我的数据集有一个字符串,一个int和一个bool,我想使用上面的generics函数来排序任何项目。 我该怎么做呢? 我现在有这个工作: if (IsString) { Expression<Func> expString = …; // call orderBy with expString } else if (IsInt) { Expression<Func> expInt; // call orderBy w/ expInt } : 我想要的东西: Expression<Func> […]

如何检查Linq查询结果中的DBNull值

我使用以下代码并尝试按货币和代码分组。 在此之后,我试图循环结果集。 但问题是在循环结果时,最后我在每个语句中得到以下exception: 无法将对象从DBNull强制转换为其他类型。 DataTable dt = new DataTable(); var result = from r in dt.AsEnumerable() result r by new { currency = r.Field(“CURRENCY”), Code = r.Field(“CODE”) } into grp select new { currency = grp.Key.currency, Code = grp.Key.Code, amount = grp.Sum(x => Convert.ToDouble(x[“AMOUNT”])) }; foreach (var obj in result) { String sCurr =obj.currency; String […]