小编Kyl*_*Ren的帖子

LINQ查询有多个StartsWith子句?

我有一个LINQ查询,它的工作原理如下,

var query = DataContext.TenantDataServerTables.Where(p => 
             p.Nursing_Home_Section == homeSection &&
             p.Tenant_Kana_Last.ToString().StartsWith(@"?") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"?") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"?") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"?") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"?"));
        }
Run Code Online (Sandbox Code Playgroud)

有没有办法做这样的事情来简化查询?

char[] array = new char[] { '?', '?', '?', '?', '?' };

var query = DataContext.TenantDataServerTables.Where(p => 
                 p.Nursing_Home_Section == homeSection &&
                 p.Tenant_Kana_Last.ToString().StartsWith(array));
Run Code Online (Sandbox Code Playgroud)

这只是一个例子,因为我必须StartsWith在LINQ查询上检查更多字符.

c# linq

2
推荐指数
3
解决办法
1940
查看次数

使用INotifyPropertyChanged的静态属性.C#

我正在尝试创建一个静态属性,其中INotifyPropertyChanged将更新DataGrid ComboBox对我绑定的任何更改.

我收到这个错误,

错误CS0026关键字'this'在静态属性,静态方法或静态字段中无效

通过我的搜索,我发现了为什么你不能在.Net的静态方法中使用关键字'this'?,但即使经历了一切,我仍然无法弄清楚如何让这个工作.

但是,我改变的任何东西都否定了我试图用INotifyPropertyChanged??? 创建一个静态属性?

我的代码:

private static List<string> _nursingHomeSectionListProperty;

public static List<string> NursingHomeSectionListProperty
{
    get { return _nursingHomeSectionListProperty; }
    set
    {
       _nursingHomeSectionListProperty = value;
       NotifyStaticPropertyChanged();
    }
}
Run Code Online (Sandbox Code Playgroud)

而Property改变了处理程序

public static event PropertyChangedEventHandler StaticPropertyChanged;

public static void NotifyStaticPropertyChanged([CallerMemberName] string propertyName = null)
    {
        StaticPropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
Run Code Online (Sandbox Code Playgroud)

以下代码是我如何使用属性更改处理程序的非静态属性,

public event PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
   {
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
Run Code Online (Sandbox Code Playgroud)

c# wpf

1
推荐指数
1
解决办法
2375
查看次数

如何实现IEnumerable <T>?

我一直试图将一个转换typeIEnumerable没有运气.

这是type我想做的IEnumerable.

public class Container<T>
{        
    public int GetInt { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是获取数据的代码,

Container<String> results = client.GetStuff(query);
Run Code Online (Sandbox Code Playgroud)

所以我试图将变量转换resultsIEnumerable<string>,但我不知道如何做到这一点.

有人能指出我正确的方向吗?

c#

1
推荐指数
1
解决办法
87
查看次数

如果语句在LINQ to SQL中

我无法弄清楚在LINQ to SQL语句中使用if子句的语法.

该语句通过从名为的列中获取唯一值来工作 Staff_No

var staffNames = sql.Staff_Time_TBLs.Select(item => 
item.Staff_No).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)

我试图从Staff_No数据等于来自名为的列的字符串中获取唯一值Section.我知道这是错的,但我希望你能得到我在C#中试图做的事情的主旨吗?

var staffNames = sql.Staff_Time_TBLs.Select(item => 
if item.Section == "Level 1" then item.Staff_No).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)

c# linq

-1
推荐指数
1
解决办法
152
查看次数

编译后的LINQ查询返回Bool值?

如何编写LINQ查询以返回Bool值?

我的代码到目前为止,

public class AddNewRow
    {
        public static Func<DatabaseDataContext, DateTime, int, Staff_Time_TBL>
            GetNewRowMissingData =
                     CompiledQuery.Compile((DatabaseDataContext db, DateTime dDate, int staffNo) =>
                     db.Staff_Time_TBLs.Any(a => a.Date_Data == dDate && a.Staff_No == staffNo));
    }
Run Code Online (Sandbox Code Playgroud)

并试过这个,

public class AddNewRow
    {
        public static Func<DatabaseDataContext, DateTime, int, Staff_Time_TBL>
            GetNewRowMissingData =
                     CompiledQuery.Compile((DatabaseDataContext db, DateTime dDate, int staffNo) =>
                     db.Staff_Time_TBLs.Where(a => a.Date_Data == dDate && a.Staff_No == staffNo).Any());
    }
Run Code Online (Sandbox Code Playgroud)

因此,如果满足两个条件,则返回true.

我试过的任何其他代码都会让帖子变得杂乱无章.

研究链接,

另外,我有一本Pro C#5.0和.NET 4.5 Framework(.NET的专家语音)这本书.

c# linq

-3
推荐指数
1
解决办法
167
查看次数

输入数组是否长于此表中的列数?

我今天整天都在收到这个错误,我看不出它是怎么发生的?我正在制作一个DataTable,它有7列,但是当我尝试将数组添加到表中时,我得到了错误.

这是代码;

    public static void BulkEntityInsert<T>(this T entity, List<T> entities)
    {
        //give array lenght
        string[] columns = new string[7];          

        DataTable dataTable = new DataTable();
        try
        {
            int jj = 0;
            DataColumn datecolumn = new DataColumn();
            foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(entity))
            {
                datecolumn.AllowDBNull = true;
                datecolumn.ColumnName = prop.Name == "Id" ? "ID" : prop.Name;
                datecolumn.DataType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                columns[jj] = prop.Name == "Id" ? "ID" : prop.Name;                    
                jj++;
            }

            foreach (T t in entities)
            {
                // give the …
Run Code Online (Sandbox Code Playgroud)

c# datatable

-9
推荐指数
1
解决办法
880
查看次数

标签 统计

c# ×6

linq ×3

datatable ×1

wpf ×1