帮助请在linq to sql中查找记录 [英] Help please finding record in linq to sql

查看:69
本文介绍了帮助请在linq to sql中查找记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试使用ling to SQL在数据库中查找记录的帮助人员,以下是我的代码,我不知道我做错了什么,有人可以帮我调查一下吗?


另外,如果我尝试查找数据库中没有的categoryID,则lblMessge弹出窗口中没有这样的条目,所以我不知道我在做什么错..请帮助任何人.

谢谢


Hello,
Help guys I''m trying to find a record in a database using ling to SQL below is my code I don''t know what I''m doing wrong can someone please help me look into it.


Also if I try to look for categoryID that isn''t in the database the lblMessge pop with no such entry so I don''t know what I''m doing wrong..help anyone please.

thanks


//instance of db
var dataContext = new DataContext();
	  
	//bring the category I want to look into
	var category = from cty in dataContext.Categories
	                 
	               //compare
	               where cty.CategoryID == int.Parse(txtSearch.Text)
	               select new
	               {
	                   CategoryName = cty.CategoryName,
	                   Picture = cty.Picture,
	                   Description = cty.Description
	               };
	 
	if (category.Count() == 0)
	{
	    lblMessage.Content = string.Format("There is no category '{0}'", txtCategoryName.Text);
	}
	else
	{
	    this.DataContext = category;
	}

推荐答案

这是LINQ,不是ling.这就是为什么您无法找到有关Google的帮助吗?当然,如果用户在txtSearch中输入非数字,则此代码会爆炸.您如何处理?

如果类别不在数据库中,则您的代码将显示该类别不存在.为什么会有问题呢?

据我所知,LINQ DataContext类没有空的构造函数,也没有Categories属性.您遇到的任何问题都存在于DataContext类及其从数据库中提取的数据中.您的LINQ代码会产生不稳定的状态,它将创建一个类别ID与键入的数字匹配的对象列表.为什么将var用于DataContext超出了我的范围.无论出什么问题,从数据库获取的数据都会出问题,而不是在此过滤代码中出问题.

我尝试的另一件事是首先将txtSearch.Text复制到另一个变量,然后在您的from语句中使用它.我想知道问题是否在于访问数据时会评估该值,而不是在首次创建类别var时会评估该值.
It''s LINQ, not ling. That may be why you can''t find help with google ? This code will, of course, blow up if a user types a non number in to txtSearch. How are you handling that ?

If the category is not in the DB, your code will show that it does not exist. Why is that a problem ?

As far as I can see, the LINQ DataContext class has no empty contructor, and no Categories property. Any issue you have, exists in your DataContext class, and in the data it pulls from the DB. Your LINQ code, shaky as it is, will create a list of objects where the categoryId matches the number typed in. Why you''re using var for the DataContext is beyond me. Whatever is going wrong, it''s going wrong in the data you get from your DB, and not in this filtering code.

Another thing I''d try, is to copy txtSearch.Text in to another variable first, and using that in your from statement. I wonder if an issue could be that the value gets evaluated when data is accessed, not when the category var is first created.


您的代码对我有效.只需仔细检查dataContext.Categories是否有记录,如果是,则使用一些静态值而不是从txtSearch.Text接受值.这是出于测试目的,稍后您将更改为从文本框中接受.

祝你好运.
Your code is valid to me. Just double check dataContext.Categories has records and If so, work with with some static value instead of accepting a value from txtSearch.Text This is for testing purpose, you will changed later to accept from the textbox.

Good luck.


这篇关于帮助请在linq to sql中查找记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆